【发布时间】:2020-06-09 17:28:17
【问题描述】:
我想使用enable_if_t 根据类型更改我的函数定义。类似这样的东西:
#include<type_traits>
template<typename T> struct A;
template<typename T>
struct A{
std::enable_if_t<std::is_arithmetic<T>::value, bool>
test()
{
return true;
}
std::enable_if_t<!std::is_arithmetic<T>::value, bool>
test()
{
return false;
}
};
【问题讨论】:
-
不允许具有相同名称和签名的类方法。函数的返回类型不是其签名的一部分。这里最简单的解决方案是将返回类型声明为
auto,并使用if constexpr返回适当的类型。
标签: c++ c++17 sfinae typetraits