好吧,这可能是一个愚蠢的问题,但我完全不明白章节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