好吧,這可能是一個愚蠢的問題,但我完全不明白章節12.1.6.2 - 在C++編程語言的constexpr函數下的條件評估。這是整個很短的文字。函數的條件評估
不評估不在constexpr 函數中採用的條件表達式的分支。這意味着沒有采取的分支可以運行時評估 。例如:
constexpr int check(int i) { return (low<=i && i<high) ? i : throw out_of_range(); } constexpr int low = 0; constexpr int high = 99; // ... constexpr int val = check(f(x,y,z));
你可能想象低,高到被 在編譯時已知的配置參數,而不是在設計時,和F(X,Y,Z) 計算一些依賴於實現值。
我試圖運行上面的代碼,試圖更多的解釋理解,但我得到一個錯誤。有人可以提供更清晰的解釋嗎?
編輯:我創建了一個程序來測試這一點:
#include<iostream>
#include<stdexcept>
using namespace std;
constexpr int low = 0;
constexpr int high = 99;
constexpr int check(int i) {
return (low<=i && i<high) ? i : throw out_of_range();
}
constexpr int f(int x, int y, int z) {
return x*y*z;
}
int main() {
constexpr int val = check(f(2,2,2));
cout << val << '\n';
}
它將無法運行:
no matching function for call to 'std::out_of_range::out_of_range()' //I'm really surprised at this
return (low<=i && i<high) ? i : throw out_of_range();
error: body of constexpr function 'constexpr int check(int)' not a return-statement
}
error: 'constexpr int check(int)' called in a constant expression
constexpr int val = check(f(2,2,2));
注意'f'也必須是'constexpr'函數。 – aschepler 2014-09-30 16:20:11
您可能想查看是否可以使用'static_assert()'。 http://en.cppreference.com/w/cpp/language/static_assert – dgnuff 2014-09-30 16:43:19
@dgnuff'static_assert' _requires_一個常量表達式,所以如果你將它添加到'check'中,你不能在運行時調用check恆定的論點。上面例子的要點是它是一個有效的運行時檢查,它也可以用來初始化constexpr變量。 – 2014-09-30 17:02:34