當我們通過一(多)派生類模板函數期待基類什麼是模板實例規則?例如:模板實例有多個模板繼承
#include <iostream>
template <int x>
struct C {};
struct D : C<0>, C<1> {};
template <int x>
void f (const C<x> &y) { std::cout << x << "\n"; }
int main()
{
f (D());
}
MSVC 2015打印0,鐺3.8 - 1和gcc 6.2給出編譯器錯誤(Demo)。而且,即使你SFINAE-了所有重載除一人外,結果仍然是不同的:
#include <iostream>
template <int x> struct C {};
template<>
struct C<0> { using type = void; };
struct D : C<0>, C<1> {};
template <int x, typename = typename C<x>::type>
void f (const C<x> &y) { std::cout << x << "\n"; }
int main()
{
f (D());
}
現在只編譯與MSVC,如果你換C<0>
和C<1>
只鐺會編譯它。問題在於MSVC只嘗試實例化第一個base,clang - last和gcc打印錯誤太早。哪個編譯器是正確的?
在我看來,好像它們都是錯的。它不應該是一個模糊的函數調用嗎? –
*「MSVC打印0,鐺-1和gcc給編譯器錯誤。」*,哪個MSVC,哪個gcc和哪個鐺? –
@PiotrSkotnicki新增版本號,但它們都具有相同的行爲 –