当我们通过一(多)派生类模板函数期待基类什么是模板实例规则?例如:模板实例有多个模板继承
#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新增版本号,但它们都具有相同的行为 –