I think in C ++ 14, more delimiters are removed from constexpr. But according to N3797 7.1.5 3-punct:
The definition of the contexpr shal function satisfies the following restrictions:
- it does not have to be virtual
- its return type must be a literal type;
- each of its parameter types must be a literal type;
- its function body must be = delete , = default or a compound statement that does not contain:
- definition of asm,
- a goto ,
- try block or
- determination of a non-literal type variable or static or storage duration of streams or for which no initialization is performed.
I know why static variables for thread storage duration are forbidden, but I see no reason why the definition of a literal variable is ONLY allowed?
Or I do not understand the standard.
I'm not sure, but according to the standard following errors, even C ++ 14 should be created:
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};
return p.hypot();
}
constexpr point getPoint(int x, int y) { return {x,y}; }
constexpr int hypot(point p) { return p.hypot(); }
Q: If the errors above occurred, why are these restrictions not removed?
source
share