首页 > 编程语言 > 详细

c++模板元编程四:IF语句编译时运行

时间:2015-04-05 17:30:11      阅读:177      评论:0      收藏:0      [点我收藏+]

2.3 if 替代

将if语句放在编译期执行,可以用模板特化的方式实现。下面是调用代码:

// test if
cout << "test if" << endl;
If<false>::Run();

输出结果为:

test if
it‘s false

模板类的实现如下:

template<bool condition>
class If {
public:
  static inline void Run() {
    cout << "it‘s true" << endl;
  }
};

template<>
class If<false> {
public:
  static inline void Run() {
    cout << "it‘s false" << endl;
  }
};

c++模板元编程四:IF语句编译时运行

原文:http://blog.csdn.net/csfreebird/article/details/44888887

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!