标签:
#条款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