3 回答
TA貢獻1811條經驗 獲得超6個贊
至少在我閱讀§7.1.5/ 3和§5.19時,以下內容可能是合法的:
unsigned constexpr const_hash(char const *input) {
return *input ?
static_cast<unsigned int>(*input) + 33 * const_hash(input + 1) :
5381;
}
這似乎遵循§7.1.5/ 3中的基本規則:
形式是:“回歸表達”;
它唯一的參數是一個指針,它是一個標量類型,因此是一個文字類型。
它的返回是unsigned int,它也是標量(因此是字面的)。
沒有隱式轉換為返回類型。
有一些問題是*inputs 是否涉及非法左值轉換,我不確定我是否理解§5.19/ 2/6/2 1和§4.1中的規則以確保這一點。
從實際的角度來看,這個代碼被(例如)g ++接受,至少可以追溯到g ++ 4.7.1。
用法如下:
switch(std::hash(value)) {
case const_hash("one"): one(); break;
case const_hash("two"): two(); break;
// ...
default: other(); break;
}
為了符合§5.19/ 2/6/2的要求,您可能需要執行以下操作:
// one of the `constexpr`s is probably redundant, but I haven't figure out which.
char constexpr * constexpr v_one = "one";
// ....
case const_hash(v_one): one(); break;
我正在使用額外的'斜線'數字來指代未編號的子彈點,所以如果是第5.19 / 2節中的第六個要點,這是內部的第二個要點。我想我可能要和Pete Becker討論是否可以在層次結構中添加某種數字/字母/羅馬數字來識別這樣的部分......
- 3 回答
- 0 關注
- 527 瀏覽
添加回答
舉報
