我想要这样的东西:
template<typename T, typename = void> // sth like T=>int or T=>double not valid
struct test : std::false_type{};
template<typename T, ???> // what should i do?
struct test<T,???> : std::true_type{};
这将导致:
std::cout << test<int>::value << std::endl;
将返回“true”和一个自定义类
class a{ int a;double b};
std::cout << test<a>::value << std::endl;
会返回“假”
最佳答案
总有一个完全显式的测试可以用于隐式转换
namespace detail
{
std::true_type check(int);
std::true_type check(float);
std::true_type check(char);
// ...
std::false_type check(...);
}
template<typename T>
struct is_implicitly_convertible_to_arithmetic
: decltype(::detail::check(std::declval<T>())) {};
这很冗长,但非常灵活。
关于c++ - 如何确定一个模板参数可以转换成算术类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49995195/