c++ - C++ SFINAE 部分特化
问题描述
我一直在尝试定义一个辅助类来帮助我使用模板方法,在这些模板方法中,我想要一个针对复杂类型和真实类型的通用实现。
到目前为止,这是我的尝试:
#include<type_traits>
#include<complex>
template<class T>
struct is_complex{ static constexpr bool value = false;};
template<class T>
struct is_complex<std::complex<T>> :
std::integral_constant<bool,
std::is_integral<T>::value ||
std::is_floating_point<T>::value>{};
template<class T>
struct is_arithmetic:
std::integral_constant<bool,
std::is_integral<T>::value ||
std::is_floating_point<T>::value ||
is_complex<T>::value>{};
template<class T,
typename std::enable_if_t<is_arithmetic<T>::value,int> =0>
struct real_type {typedef T type;};
template<class T>
struct real_type<typename std::complex<T>>{typedef T type;};
我想得到类似的东西
typename real_type<std::complex<double>> myVar1;//myVar1 is double
typename real_type<double> myVar2;//myVar2 is double
只要我不在乎非算术类型也有real_type<T>::type
. 但是现在我已经添加了这个额外的约束,我不能让它工作,我真的不明白为什么。
澄清一下:我希望调用 likereal_type<std::string>::type
会产生编译时错误。我希望这些调用仅对算术(包括复数)和整数类型有效。
我最近尝试的编译器错误是:
non-type template argument specializes a template parameter with dependent type 'typename std::enable_if_t<is_arithmetic<T>::value, int>' (aka 'typename enable_if<is_arithmetic<T>::value, int>::type')
但我不知道如何处理它。如果此信息有用,我可以访问支持 C++17 的编译器。
解决方案
通常这是通过专业化和模板默认参数来完成的。
我是说
template <typename, typename = void>
struct real_type;
template <typename T>
struct real_type<T, std::enable_if_t<std::is_arithmetic_v<T>>>
{ using type = T; };
template <typename T>
struct real_type<std::complex<T>, void>
{ using type = T; };
std::complex
正如帕特里克·罗伯茨(Patrick Roberts)所观察到的(谢谢),std::complex
您对 and 有单独的专业化,而不会is_arithmetic
成为的副本std::is_arithmetic
(所以最好直接使用std::is_arithmetic
)。
你得到
real_type<int> r1; // compile
real_type<std::complex<float>> r2; // compile
//real_type<std::string> r3; // compilation error