问题描述
当我们将(多)派生类传递给期望基类的模板函数时,模板实例化的规则是什么?例如:
What are the rules for template instantiation when we pass a (multi)derived class to a template function expecting base class? For example:
#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,clang 3.8 - 1和gcc 6.2给出编译器错误)。即使你SFINAE除去一个以外的所有重载,结果仍然会有所不同:
MSVC 2015 prints 0, clang 3.8 - 1 and gcc 6.2 gives compiler error (Demo). And even if you SFINAE-away all overloads except one, the result will still be different:
#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
和 C
只有clang才能编译它。问题是MSVC只试图实例化第一个基础,clang - last和gcc打印错误太早。哪个编译器是正确的?
Now it compiles only with MSVC, and if you swap C<0>
and C<1>
only clang will compile it. The problem is that MSVC only tries to instantiate first base, clang - last and gcc prints error too early. Which compiler is right?
推荐答案
gcc 5.4:
/tmp/gcc-explorer-compiler11685-58-1h67lnf/example.cpp: In function 'int main()':
13 : error: no matching function for call to 'f(D)'
f (D ());
^
9 : note: candidate: template<int x> void f(const C<x>&)
void f (const C<x> &y) { std::cout << x << "\n"; }
^
9 : note: template argument deduction/substitution failed:
13 : note: 'const C<x>' is an ambiguous base class of 'D'
f (D ());
^
Compilation failed
这似乎是正确的结果,因为C 0和C 1同等地专门化。
Which seems to me to be the correct result, since C<0> and C<1> are equally specialised.
对于gcc 6.2的相同结果
Same result for gcc 6.2
clang
更新:
我不能编译它, t知道实际的用例,但我想知道这是否可以为你工作:
I don't know the actual use case but I was wonder whether this might work for you:
#include <utility>
#include <iostream>
template<class T>
struct has_type
{
template<class U> static auto test(U*) -> decltype(typename U::type{}, std::true_type());
static auto test(...) -> decltype(std::false_type());
using type = decltype(test((T*)0));
static const auto value = type::value;
};
template <int x> struct C {};
template<>
struct C<0> { using type = int; };
template<int...xs>
struct enumerates_C : C<xs>...
{
};
struct D : enumerates_C<0, 1> {};
template<int x, std::enable_if_t<has_type<C<x>>::value>* = nullptr>
void f_impl(const C<x>& y)
{
std::cout << x << "\n";
}
template<int x, std::enable_if_t<not has_type<C<x>>::value>* = nullptr>
void f_impl(const C<x>& y)
{
// do nothing
}
template <int...xs>
void f (const enumerates_C<xs...> &y)
{
using expand = int[];
void(expand { 0,
(f_impl(static_cast<C<xs> const &>(y)),0)...
});
}
int main ()
{
f (D ());
}
预期输出(对苹果(测试):
expected output (tested on apple clang):
0
这篇关于具有多个模板继承的模板实例化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!