C++部分专业化

C++部分专业化,c++,c++17,sfinae,template-specialization,C++,C++17,Sfinae,Template Specialization,我一直在尝试定义一个辅助类来帮助我使用模板方法,在这些方法中,我希望为复杂类型和实际类型提供一个通用实现 到目前为止,我一直在尝试: #include<type_traits> #include<complex> template<class T> struct is_complex{ static constexpr bool value = false;}; template<class T> struct is_complex<s

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

到目前为止,我一直在尝试:

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

澄清一下:我希望像real_type::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; };

您能发布生成您描述的编译器错误的代码吗?这是我在问题中输入的代码。非常感谢您的回答@mattlangford!我试图在我的问题中更好地解释它,但我的意思是,我希望像real_type::type这样的调用会导致编译时错误。但是你从V中得到T的想法可能非常有用,即使我必须为我感兴趣的每一个V类分别专门化它。我稍微更新了一下,这样它就应该符合你的想法!有趣的是,您仍然需要对std::string进行重写,因为std::string基本上是一个std::vector,char是算术std::string是一个std::basic_字符串。我认为这里不需要模板参数,因为std::complex是唯一可以专门处理的相关模板类。@PatrickRoberts-很好。答案很简单。谢谢,非常感谢。这个非常适合我,而且看起来非常干净。我只需要添加std::is_integral_v,我可以在代码中直接使用它。
real_type<int>                 r1;   // compile
real_type<std::complex<float>> r2;   // compile
//real_type<std::string>         r3; // compilation error