现在模拟switch/case语句,不过也是在编译期运行。先看调用代码和输出结果
// test case cout << "test case" << endl; Case<2>::Run();
test case case 2
实现代码很简单,还是模板特化
template<int v> class Case { public: static inline void Run() { cout << "default case" << endl; } }; template<> class Case<1> { public: static inline void Run() { cout << "case 1" << endl; } }; template<> class Case<2> { public: static inline void Run() { cout << "case 2" << endl; } };
原文地址:http://blog.csdn.net/csfreebird/article/details/44891047