【发布时间】:2014-03-14 07:03:17
【问题描述】:
我认为在 C++14 中,从 constexpr 中删除了更多限制。但根据 N3797 7.1.5 3-punct:
conexpr 函数的定义应满足以下约束:
- 它不能是虚拟的
- 它的返回类型应该是文字类型;
- 它的每个参数类型都应该是文字类型;
- 它的 function-body 应该是 = delete、= default 或一个 compound-statement不包含:
- asm 定义,
- goto 语句,
- 一个try-block,或者
- 非文字类型或静态或线程存储持续时间的变量的定义,或者没有初始化的变量的定义 执行。
我知道为什么不允许使用静态、线程存储持续时间变量,但我看不出任何原因,为什么只允许定义文字类型的变量?
或者我不理解标准。
我不确定,但根据标准,即使 C++14 也应该创建以下错误:
struct point{
constexpr point(): x(0), y(0){}
constexpr point(int x_, int y_): x(x_),y(y_){}
constexpr int hypot()const { return x*x + y*y; }
int x,y;
};
constexpr int hypot(int x, int y) {
point p{x,y}; //error, because p - is not literal type.
return p.hypot();
}
// error, because return type is not literal.
constexpr point getPoint(int x, int y) { return {x,y}; }
// error, because parameter is not literal.
constexpr int hypot(point p) { return p.hypot(); }
问:如果真的会发生上述错误,为什么不取消这些限制?
【问题讨论】:
标签: c++ c++11 language-lawyer constexpr c++14