C++ SFINAE 偏特化

C++ SFINAE partial specialization

我一直在尝试定义一个辅助 class 来帮助我使用模板方法,我希望在其中为复杂类型和真实类型提供通用实现。

到目前为止,这是我的尝试:

#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,我就能让它工作。但是现在我已经添加了这个额外的约束,我无法让它工作而且我真的不明白为什么。

澄清一下:我希望像 real_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 的编译器。

我可以建议一个更简洁、更通用的实现(需要 c++17):

#include <type_traits>

// catch all for single parameters that have 0 template parameters 
template <typename T>
struct real_type{
    static_assert(std::is_arithmetic_v<T>);
    using type = T;
};
template <typename T>
using real_type_t = typename real_type<T>::type;

// magically catch anything which matches V<T, Ts...> and exposes T as `type`
template <template <typename...> typename V, typename T, typename...Ts>
struct real_type<V<T, Ts...>>{
    using type = real_type_t<T>;
};

#include <vector>
#include <complex>

using d = real_type_t<double>;
static_assert(std::is_same_v<d, double>);
using d2 = real_type_t<std::vector<double>>;
static_assert(std::is_same_v<d2, double>);
using d3 = real_type_t<std::complex<double>>;
static_assert(std::is_same_v<d3, double>);

// doesn't compile
struct NotValid {};
using d4 = real_type_t<std::vector<NotValid>>;

通常这是通过专业化和模板默认参数完成的。

我是说

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,您的 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