您可以在不专门化类模板参数的情况下专门化模板类中的模板方法吗?
请注意,特化是模板参数的值,而不是它的类型。
这似乎在 Visual Studio 2008 SP1 编译器下编译,但不是 GCC 4.2.4。
#include <iostream>
using namespace std;
template <typename T>
class A
{
private:
template <bool b>
void testme();
template <>
void testme<true>() { cout << "true" << endl; };
template <>
void testme<false>() { cout << "false" << endl; };
public:
void test();
};
template<typename T> struct select {};
template<> struct select<int> { static const bool value = true; };
template<> struct select<double> { static const bool value = false; };
template <class T>
void A<T>::test() { testme<select<T>::value>(); }
int main(int argc, const char* argv[])
{
A<int> aInt;
A<double> aDouble;
aInt.test();
aDouble.test();
return 0;
}
GCC 告诉我:“错误:非命名空间范围 'class A' 中的显式特化”
如果标准不支持,谁能告诉我为什么?