让std :: complex <double>传递std :: is_floating_point测试

use*_*717 5 c++ templates static-assert c++11

我想要的类型double,float,complex<double>complex<float>通过一个static_assert条件.我认为static_assert(std::is_floating<T>::value, "some message")可以做到这一点,但是复杂的类型没有通过这个测试(至少在gcc-4.10下).

我将添加什么谓词以确保long double允许这四种类型(也许也可以是s)作为模板实例化,但没有别的?

T.C*_*.C. 10

为标准库类型特征类添加特化通常是非法的,即使对于用户定义的类型也是如此.§20.10.2[meta.type.synop]/p1:

除非另有说明,否则为本子条款中定义的任何类模板添加特殊化的程序的行为是未定义的.

目前,唯一允许用户添加特化的类型特征类是std::common_type,如果特化中的至少一个模板参数是用户定义的类型(§20.10.7.6[meta.trans.other],表57).

你需要写自己的特质,这并不难:

template<class T>
struct is_complex_or_floating_point : std::is_floating_point<T> { };

template<class T>
struct is_complex_or_floating_point<std::complex<T>> : std::is_floating_point<T> { };
Run Code Online (Sandbox Code Playgroud)

演示.