This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 110b. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.

2023-01-15


2400. Constexpr virtual functions and temporary objects

Section: 7.7  [expr.const]     Status: CD5     Submitter: Daveed Vandevoorde     Date: 2019-02-08

[Accepted as a DR at the July, 2019 meeting.]

Consider an example like the following:

  struct A {
    constexpr virtual int f() const {
      return 1;
    }
  };

  struct B : A {
    constexpr virtual int f() const {
      return 2;
    }
  };

  constexpr B b{};
  constexpr A&& ref = (B)b;

  static_assert(ref.f() == 2, "");

Since the temporary bound to ref is non-const, it can be re-newed to something else, which would make the invocation ref.f() undefined behavior, which the interpreter is required to catch.

Presumably, ref.f() should not be a constant expression, and 7.7 [expr.const] paragraph 2 should have a bullet for invoking a virtual function of a non-const object unless its lifetime began within the evaluation of the constant expression.

Proposed resolution (March, 2019):

Add the following as a new bullet following 7.7 [expr.const] bullet 4.4:

An expression e is a core constant expression unless the evaluation of e, following the rules of the abstract machine (6.9.1 [intro.execution]), would evaluate one of the following expressions: