继承特化的模板参数推导
Template argument deduction for inheriting specializations
考虑 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>
isaBB<int>
,那为什么只有BB<float>
不能被选中呢?!
问题是:这是否符合标准?在定义 ff
或 BB
以帮助 gcc-8.3
选择 BB<float>
时是否有解决方法?
此问题是 CWG 2303. The committee decided to add wording "preferring 'nearer' base classes" and this wording was added to the working draft 的主题。因此,在 C++20 中,您的示例应该实例化 ff<float>(BB<float>)
,而在 C++17 中,它是不明确的。
当然,如果您的编译器不支持 "C++2a" 模式或 C++2a 模式尚未实现此更改,解决方法是添加 ff
的重载需要 D
.
考虑 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>
isaBB<int>
,那为什么只有BB<float>
不能被选中呢?!
问题是:这是否符合标准?在定义 ff
或 BB
以帮助 gcc-8.3
选择 BB<float>
时是否有解决方法?
此问题是 CWG 2303. The committee decided to add wording "preferring 'nearer' base classes" and this wording was added to the working draft 的主题。因此,在 C++20 中,您的示例应该实例化 ff<float>(BB<float>)
,而在 C++17 中,它是不明确的。
当然,如果您的编译器不支持 "C++2a" 模式或 C++2a 模式尚未实现此更改,解决方法是添加 ff
的重载需要 D
.