以下内容-1
在GCC 4.9,VC ++中打印2
,但在Clang 3.5中打印:
template <int n>
struct base { static constexpr int value = n; };
struct A : base<0> { };
struct B : base<1> { };
struct C : base<2> { };
struct D : A, B, C { };
template <int n>
base<n> base_cast(base<n>);
template <typename T, typename B = decltype(base_cast(std::declval<T>()))>
std::integral_constant<int, B::value> f(int);
template <typename T>
std::integral_constant<int, -1> f(...);
constexpr int value = decltype(f<D>(0))::value;
int main()
{
std::cout << value << std::endl;
}
我说铛不正确,因为有多个转换D
到base<n>
:通过A
,B
和C
。clang所做的奇怪的事情是,它似乎转换为其基本说明符列表中的最后一个基类。
这是Clang中的错误吗?我可以从标准中获得实际用语吗?
标准中的第4条涉及标准转换:
4.2 [注:具有给定类型的表达式将在几种情况下隐式转换为其他类型:
—用作初始化的源表达式(包括用作函数调用中的参数和用作return语句中的表达式)。被初始化的实体的类型(通常)是目标类型。见8.5,8.5.3。
—尾注]
4.3当且仅当声明T t = e时,表达式e才能隐式转换为类型T;格式正确,对于某些发明的临时变量t(8.5)
基于此D可以转换为base <0>,base <1>和base <2>。
base<0> b0 = D(); //compiles
base<1> b1 = D(); //compiles
base<2> b2 = D(); //compiles
由于存在三种可能的转换,因此对base_cast的调用应导致模棱两可的调用。
以下代码段:
template <int n>
void print_base(base<n>)
{
std::cout << n << std::endl;
}
...
print_base(D());
在gcc 4.7.2中给出以下错误:
error: no matching function for call to 'print_base(D)'
note: candidate is:
note: template<int n> void print_base(base<n>)
note: template argument deduction/substitution failed:
note: 'D' is an ambiguous base class of 'base<n>'
错误消息在这里似乎有点偏离:'D'是base <n>的模棱两可的“ base”类,但是它传达了这一点。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句