Can have a non-literal type variable in constexpr function body C ++ 14?

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}; //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(); }

Q: If the errors above occurred, why are these restrictions not removed?

+4
source share
1 answer

The literal type is defined in 3.9 / 10:

A type is a literal type if it:

  • void; or

  • scalar type; or

  • reference type; or

  • an array of type literal; or

  • class type (section 9), which has all of the following properties:

    • has a trivial destructor,

    • (8.5.1) constexpr, ,

, point , ++ 1y.

, constexpr literal, , .

+9

Source: https://habr.com/ru/post/1527008/


All Articles