enum testEnum {A, B, C};template testEnum Cclass TestClassTemplate {public: void print() { printf("common\n"); }};template class TestClassTemplateA {public: void print() { printf("specific int\n"); }};int main() { TestClassTemplateB doubleC
enum testEnum {A, B, C};
template <testEnum C>
class TestClassTemplate {
public:
void print() {
printf("common\n");
}
};
template <>
class TestClassTemplate<A> {
public:
void print() {
printf("specific int\n");
}
};
int main() {
TestClassTemplate<B> doubleClassTemplate;
TestClassTemplate<A> intClassTemplate;
doubleClassTemplate.print();
intClassTemplate.print();
}
//Res:
//common
//specific int
C++ 模板使用 enum 取代 typename/class
察看 gnu的shared_ptr实现,偶然发现这样:
// Empty helper class except when the template argument is _S_mutex.
template<_Lock_policy _Lp>
class _Mutex_base
{
protected:
// The atomic policy uses fully-fenced builtins, single doesn't care.
enum { _S_need_barriers = 0 };
};
template<_Lock_policy _Lp> 对这一句,有些疑惑,自己印象里template 后面跟的都是 <class/typename A> 或者<>, 但是还没见过这种的,
后来跟发现:
enum _Lock_policy { _S_single, _S_mutex, _S_atomic }; 原来是个enum, 没想到enum还能这么用,于是就有上了上面的例子.
应该属于比较偏门的用法吧?