5

In section 10.4.3 of his new book "TCPL", B. Stroustrup writes:

A sufficiently simple user-defined type can be used in a constant expression. For example:

struct Point {
    int x,y,z;
    constexpr Point up(int d) { return {x,y,z+d}; }
    constexpr Point move(int dx, int dy) { return {x+dx,y+dy}; }
// ...
};

A class with a constexpr constructor is called a literal type. To be simple enough to be constexpr, a constructor must have an empty body and all members must be initialized by potentially constant expressions. For example:

constexpr Point origo {0,0};

This seems confusing to me for the following reasons:

  • struct Point has no user defined constructor, nor its implicit default constructor is constexpr.
  • constexpr Point origo {0,0}; compiles because of paragraph 7.1.5/9 in the Standard (N3337), concerning the use of constexpr in object declarations and paragraph 8.5.1/7, concerning aggregates initialization. It has nothing to do with a constexpr constructor.
4
  • "nor its implicit default constructor is constexpr" Why shouldn't the implicitly-declared (&defined) default ctor be constexpr?
    – dyp
    Mar 5, 2014 at 18:40
  • I agree with your second point: There's no constructor call in the aggregate-initialization Point{0,0}.
    – dyp
    Mar 5, 2014 at 18:45
  • 1
    @dyp Why shouldn't the implicitly-declared (&defined) default ctor be constexpr? Members x, y and z are not initialized. Mar 5, 2014 at 19:38
  • Agree with both points now :)
    – dyp
    Mar 5, 2014 at 19:48

1 Answer 1

0

There is no requirement of a user-defined constructor. The text says "user-defined type", which is true, and "constructor must have an empty body", which is true (default constructor is equivalent to one with an empty body).

Also constexpr is used to indicate that the result is a compile-time constant. It doesn't allow/disallow any particular syntax on functions, it just allows the compiler to verify at compile-time that a constant value is being returned as expected. The constexpr on the functions and declaration indicate only that the functions are returning a compile-time constant.

Edit: Oh, also, I think you might be linking a few independent statements in that quote. The first sentence and code snippet is illustrating how the user-defined type Point can be used as a return value for a constexpr function. The second bit is illustrating that Point can be used as a constexpr variable because its constructor is empty; the {0,0} syntax itself isn't specifically related to constexpr constructors but it satisfies the requirements for constexpr variables where "a constructor must have an empty body and all members must be initialized by potentially constant expressions". See here for a good overview of the variable/function/constructor terminology used with constexpr.

4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.