【发布时间】:2012-10-26 10:02:01
【问题描述】:
给定一个类声明
class A {
template <typename T> T foo();
};
我想将A::foo 专门用于T 的各种类型(int,...)和类型类(POD,非 POD)。不幸的是,我似乎不能为后者使用std::enable_if。以下内容无法编译:
template <> int A::foo<int>(); // OK
template <typename T>
typename std::enable_if<is_pod<T>::value, T>::type foo(); // <<<< NOT OK!
template <typename T>
typename std::enable_if<!is_pod<T>::value, T>::type foo(); // <<<< NOT OK!
问题可能是由于 std::enable_if<...> 是函数签名的一部分,并且我没有在 A 中声明任何此类成员。那么如何根据类型特征对模板成员进行专门化呢?
【问题讨论】:
标签: c++ templates c++11 template-specialization