问题描述
在下面的代码中,编译器为B
选择模板重载Foo<float>
,为所有其他类型选择模板重载A
,例如Foo<char>
。这似乎取决于重载B
中的第二个模板参数是否与void
初始定义中该模板参数的指定默认值Foo
匹配。
我不了解其背后的机制。请解释这个法术。
template <typename T>
struct Bar
{
using type = int;
};
template <>
struct Bar<float>
{
using type = void;
};
// overload A
template <typename T1,typename T2=void>
struct Foo
{
using type = int;
};
// overload B
template <typename T>
struct Foo<T,typename Bar<T>::type>
{
using type = float;
};
int main()
{
[[maybe_unused]] Foo<float>::type foo1 {2.3}; // compiler picks overload B
[[maybe_unused]] Foo<char>::type foo2 {2}; // compiler picks overload A
return 0;
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)