获取std :: complex< double>传递std :: is_floating_point测试 [英] Getting std::complex<double> to pass std::is_floating_point test
问题描述
我想要类型 double
, float
, complex< double>
和 complex< float>
以传递 static_assert
条件。我想到 static_assert(std :: is_floating< T> :: value,一些消息)
会做的技巧,但复杂类型不通过这个测试下gcc-4.10)。
I want the types double
, float
, complex<double>
and complex<float>
to pass a static_assert
condition. I figured static_assert(std::is_floating<T>::value, "some message")
would do the trick, but the complex types do not pass this test (at least under gcc-4.10).
我需要添加什么谓词,以确保这四种类型(也许 long double
作为模板实例化,但没有别的?
What predicate would I add to make sure these four types (and perhaps long double
s as well) are allowed as template instantiations, but nothing else?
推荐答案
定义类型。 §20.10.2[meta.type.synop] / p1:
It is generally illegal to add specializations for standard library type trait classes, even for user-defined types. §20.10.2 [meta.type.synop]/p1:
程序的行为为任何$ b
The behavior of a program that adds specializations for any of the class templates defined in this subclause is undefined unless otherwise specified.
目前,唯一的类型trait类,用户如果专业化中至少有一个模板参数是用户定义的类型(§20.10.7.6[meta.trans。]),则允许为 std :: common_type
其他],表57)。
Currently, the only type trait class that users are allowed to add specializations for is std::common_type
, if at least one template parameter in the specialization is a user-defined type (§20.10.7.6 [meta.trans.other], Table 57).
您需要编写自己的trait,这并不困难:
You need to write your own trait, which isn't hard:
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> { };
演示 。
这篇关于获取std :: complex< double>传递std :: is_floating_point测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!