我希望double、float、complex<double>和complex<float>类型传递static_assert条件。我认为static_assert(std::is_floating<T>::value, "some message")可以做到这一点,但是复杂的类型不能通过这个测试(至少在gcc-4.10下是这样)。
我应该添加什么谓词来确保这四种类型(可能也包括long double)被允许作为模板实例化,但不允许使用其他谓词?
发布于 2014-08-17 00:50:52
为标准库类型特征类添加专门化通常是非法的,甚至对于用户定义的类型也是非法的。§20.10.2元..20.10.2/p1:
除非另有规定,否则为该子子句中定义的任何类模板添加专门化的程序的行为是未定义的。
目前,如果专门化中至少有一个模板参数是用户定义的类型(§20.10.7.6 meta.trans.other,表57),则允许用户为其添加专门化的类型特性类为std::common_type。
你需要写出你自己的特质,这并不难:
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> { };演示。
https://stackoverflow.com/questions/25345206
复制相似问题