您的位置:首页 > 编程语言 > C语言/C++

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

2014-04-02 12:06 537 查看
有时候,我们想写出下面这样的switch语句:

const char* str = "first";
switch(str){
case "first":
cout << "1st one" << endl;
break;
case "second":
cout << "2nd one" << endl;
break;
case "third":
cout << "3rd one" << endl;
break;
default:
cout << "Default..." << endl;
}


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

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

基本思路为:

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

typedef std::uint64_t hash_t;

constexpr hash_t prime = 0x100000001B3ull;
constexpr hash_t basis = 0xCBF29CE484222325ull;

hash_t hash_(char const* str)
{
hash_t ret{basis};

while(*str){
ret ^= *str;
ret *= prime;
str++;
}

return ret;
}


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

constexpr hash_t hash_compile_time(char const* str, hash_t last_value = basis)
{
return *str ? hash_compile_time(str+1, (*str ^ last_value) * prime) : last_value;
}


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

void simple_switch2(char const* str)
{
using namespace std;
switch(hash_(str)){
case hash_compile_time("first"):
cout << "1st one" << endl;
break;
case hash_compile_time("second"):
cout << "2nd one" << endl;
break;
case hash_compile_time("third"):
cout << "3rd one" << endl;
break;
default:
cout << "Default..." << endl;
}
}


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

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

constexpr unsigned long long operator "" _hash(char const* p, size_t)
{
return hash_compile_time(p);
}


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

"first"_hash


现在,我们写出的switch语句就好看多了。
void simple_switch(char const* str)
{
using namespace std;
switch(hash_(str)){
case "first"_hash:
cout << "1st one" << endl;
break;
case "second"_hash:
cout << "2nd one" << endl;
break;
case "third"_hash:
cout << "3rd one" << endl;
break;
default:
cout << "Default..." << endl;
}
}


全文完。希望我讲清楚了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐