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

2024-12-19


2962. Evaluation of destructor call for variable with constant destruction

Section: 7.7  [expr.const]     Status: open     Submitter: Cody Miller     Date: 2024-11-23

Consider:

  #include <type_traits>

  extern "C" int puts(const char*);

  struct S {
    consteval S(int i) : buf{i} {}
    constexpr ~S() {
      if (!std::is_constant_evaluated())
	puts("Destroyed\n");
    }
    int buf;
  };

  int main() {
    constexpr S s{43};
    return s.buf;
  }

The destruction should be manifestly constant-evaluated.

Also consider:

  struct A {
    constexpr A() {
      if !consteval { puts("constructed"); }
    }
    constexpr ~A() {
      if !consteval { puts("destroyed"); }
    }
  };

  void f() {
    { A a; }
    delete new A;
    A();
  }

  void g() {
    constexpr A a;      // Usable in constant expressions: destruction is manifestly constant evaluated.
  }
  A a;      // Constant-initialized, but destruction is not manifestly constant evaluated.

Possible resolution:

Change in 7.7 [expr.const] paragraph 21 as follows:

An expression or conversion is manifestly constant-evaluated if it is: