码迷,mamicode.com
首页 > 编程语言 > 详细

Effective C++学习--条款2(少用#define)

时间:2015-05-25 13:03:39      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:

#条款2

尽量使用consts,enums,template inline替代#define

 1 #include <iostream>
 2 #include <string>
 3 #include <functional>
 4 
 5 using namespace std;
 6 bool check_size(const string &s, string::size_type sz);
 7 
 8 template<class T>
 9 void f(T& a){
10 
11 }
12 
13 template<class T>
14 inline void CALL_WITH_MAX(const T& a, const T& b){
15     f(a > b ? a : b);
16 }
17 
18 
19 bool isShorter(const string &a, const string &b){
20     return a.size() < b.size();
21 }
22 
23 int main(int argc, char **argv){
24     string s = "hello";
25     int i = 9;
26     auto check6 = bind(check_size, std::placeholders::_1, std::ref(i));
27     bool b1 = check6(s);
28     int a = 3, b = 5;
29     CALL_WITH_MAX(a, b);
30     system("pause");
31     return 0;
32 }
33 
34 bool check_size(const string &s, string::size_type sz){
35     cout << "Call check_size" << endl;
36     return s.size() >= sz;
37 }

其他代码是使用bind的测试。

 

Effective C++学习--条款2(少用#define)

标签:

原文地址:http://www.cnblogs.com/lhyz/p/4527561.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!