7

When C++14 lifted restrictions on constexpr it seemed to include the following (copied from Wikipedia):

Expressions may change the value of an object if the lifetime of that object began within the constant expression function. This includes calls to any non-const constexpr-declared non-static member functions.

That seems to imply that you could create an object using new and as long as you delete it within the expression, then it would be allowed.

TemplateRex
  • 69,038
  • 19
  • 164
  • 304
Glenn Teitelbaum
  • 10,108
  • 3
  • 36
  • 80

2 Answers2

9

Language lawyer answer. All references to N3797.

7.1.5/5 states:

For a non-template, non-defaulted constexpr function or a non-template, non-defaulted, non-inheriting constexpr constructor, if no argument values exist such that an invocation of the function or constructor could be an evaluated subexpression of a core constant expression (5.19), the program is ill-formed; no diagnostic required.

Jumping over to 5.19, we see:

A conditional-expression e is a core constant expression unless the evaluation of e, following the rules of the abstract machine (1.9), would evaluate one of the following expressions:

  • ... [lots of bullets]...

  • a new-expression (5.3.4);

  • a delete-expression (5.3.5);

  • ... [lots more bullets]...

So no: a program containing a constexpr function with an unconditional invocation of new or delete in it is ill-formed, no diagnostic required. (I'd be surprised, however, if any half-decent compiler failed to diagnose such invocations of new or delete in a constexpr function, required or not.)

Community
  • 1
  • 1
Casey
  • 41,449
  • 7
  • 95
  • 125
  • 3
    *I'd be surprised, however, if any half-decent compiler failed to diagnose instances of new or delete in a constexpr function, required or not.* It's not a problem to have a `new` or `delete` in a constexpr function. This one for example is perfectly legal (although silly) `constexpr int f(int a) { return a ? a : *new int(0); }` As long as called with non-zero ints, we are fine. No diagnostic is required and the code is ill-formed for the case where for *all function arguments* the `new` will be evaluated. – Johannes Schaub - litb Mar 04 '14 at 00:00
  • 1
    update, year 2018: both clang and gcc still silently accept buggy code.... – NoSenseEtAl Jul 20 '18 at 18:51
2

I don't think so. You are still limited to calling other constexpr functions. new is actually a function call to operator new() which is not a constexpr function. The same goes for delete.

Ferruccio
  • 98,941
  • 38
  • 226
  • 299
  • A call to `operator new()` is one of several effects of using the `new` operator. (Another important effect is running the object constructor). Ditto `delete`, which calls the destructor before the deallocation function. – Ben Voigt Dec 21 '14 at 18:56