使用C++11新特性,实现用字符串作为switch的case子句(转)

来源:互联网 发布:淘宝心选怎么没有了 编辑:程序博客网 时间:2024/05/29 09:03
转自:http://blog.csdn.net/yozidream/article/details/22789147
C++的switch没有使用字符串作为case选择分支的。所以这里用这个作为字符串的case分支真的很不错。因为这里用到了C++11constexpr函数文字常量语法,函数会在编译的时候生成字串符的hash值,所以不会出现case重复的情况,如果出现重复程序会编译报错。
下边是原文:

有时候,我们想写出下面这样的switch语句:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. const char* str = "first";  
  2. switch(str){  
  3. case "first":  
  4.     cout << "1st one" << endl;  
  5.     break;  
  6. case "second":  
  7.     cout << "2nd one" << endl;  
  8.     break;  
  9. case "third":  
  10.     cout << "3rd one" << endl;  
  11.     break;  
  12. default:  
  13.     cout << "Default..." << endl;  
  14. }  

但是在c++中,是不能用字符串来作为case的标签的;于是,很疑惑,我们只能用其他的办法来实现这样的需求。

但幸运的是,c++11引入了constexpr和自定义文字常量,将这两个新特性结合,我们实现出看上去像上面这样的代码。


基本思路为:

1、定义一个hash函数,计算出字符串的hash值,将字符串转换为1个整数;

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. typedef std::uint64_t hash_t;  
  2.    
  3. constexpr hash_t prime = 0x100000001B3ull;  
  4. constexpr hash_t basis = 0xCBF29CE484222325ull;  
  5.   
  6. hash_t hash_(char const* str)  
  7. {  
  8.     hash_t ret{basis};  
  9.    
  10.     while(*str){  
  11.         ret ^= *str;  
  12.         ret *= prime;  
  13.         str++;  
  14.     }  
  15.    
  16.     return ret;  
  17. }  

2、利用c++11自定义文字常量的语法,定义一个constexpr函数,switch的case标签处调用这个constexpr函数。如下

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. constexpr hash_t hash_compile_time(char const* str, hash_t last_value = basis)  
  2. {  
  3.     return *str ? hash_compile_time(str+1, (*str ^ last_value) * prime) : last_value;  
  4. }  


这个函数只有短短的一行,利用递归得到了与上面hash_函数得到的同样值,由于用constexpr声明了函数,因此编译器可以在编译期得出一个字符串的hash值,而这正是关键,既然是编译器就可以得到的整型常量,自然可以放到switch的case标签处了。

于是我们可以写出这样的swich语句:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. void simple_switch2(char const* str)  
  2. {  
  3.     using namespace std;  
  4.     switch(hash_(str)){  
  5.     case hash_compile_time("first"):  
  6.         cout << "1st one" << endl;  
  7.         break;  
  8.     case hash_compile_time("second"):  
  9.         cout << "2nd one" << endl;  
  10.         break;  
  11.     case hash_compile_time("third"):  
  12.         cout << "3rd one" << endl;  
  13.         break;  
  14.     default:  
  15.         cout << "Default..." << endl;  
  16.     }  
  17. }  

这个实现中,hash_compile_time("first")是编译器计算出来的一个常量,因此可以用作case标签;而且如果出现了hash值冲突,编译器回给出错误提示。


3、上面的语法还不够漂亮,利用自定义文字常量,重载一个operator如下:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. constexpr unsigned long long operator "" _hash(char const* p, size_t)  
  2. {  
  3.     return hash_compile_time(p);  
  4. }  

现在我们可以写这样的文字常量,用“_hash”作为自定义文字常量的后缀,编译器看到这个后缀结尾的文字常量,就会去调用我们重载的operator,得到和调用hash_compile_time是一样的值,但看起来舒服多了:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. "first"_hash  


现在,我们写出的switch语句就好看多了。

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. void simple_switch(char const* str)  
  2. {  
  3.     using namespace std;  
  4.     switch(hash_(str)){  
  5.     case "first"_hash:  
  6.         cout << "1st one" << endl;  
  7.         break;  
  8.     case "second"_hash:  
  9.         cout << "2nd one" << endl;  
  10.         break;  
  11.     case "third"_hash:  
  12.         cout << "3rd one" << endl;  
  13.         break;  
  14.     default:  
  15.         cout << "Default..." << endl;  
  16.     }  
  17. }  
  18.    

全文完。希望我讲清楚了。
经过我的测试,在cocos2d-x 3.2版本里,我对骨骼动画的帧事件中用了switch,可以正常使用。