【发布时间】:2019-08-16 18:22:51
【问题描述】:
考虑this代码:
#include <type_traits>
template < typename > struct BB { };
template < > struct BB<float> : BB<int> { };
struct DD : BB<float> { };
template < typename... Args >
void ff(BB<Args...>) { }
int main()
{
ff(BB<float>{});
ff(DD{}); // FAILS! 'BB<Args ...>' is an ambiguous base class of 'DD'
return 0;
}
ff(DD{}) 的调用无法编译,因为gcc-8.3 不想从BB<float> 和BB<int> 中选择一个(clang 也是如此)。但是BB<float> isa BB<int>,那为什么不能只选择BB<float>?!
问题是:这是否符合标准?在定义ff 或BB 时是否有解决方法来帮助gcc-8.3 选择BB<float>?
【问题讨论】:
-
看起来这是一个 gcc 错误。对于它的价值,icc19 编译得很好。我看不出为什么 DD 不能转换为
BB<float>。 -
@SergeyA:为什么编译器应该能够正确推断出这一点?怎么样?
-
与此处相同:godbolt.org/z/bPYajH
-
@SergeyA:但是,MSVC 会处理它
-
@SergeyA:我认为 [temp.deduct.type] 同意你的观点,应该可以推导出模板参数。
标签: c++ templates inheritance language-lawyer specialization