Title
Evaluation of destructor call for variable with constant destruction
Status
open
Section
7.7 [expr.const]
Submitter
Cody Miller

Created on 2024-11-23.00:00:00 last changed 2 weeks ago

Messages

Date: 2024-12-01.22:23:31

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:
  • ...
  • the initializer of a variable that is usable in constant expressions or has constant initialization (6.9.3.2 [basic.start.static]). , [ Footnote: ... ] or [ Example: ... ]
  • the destruction of an object that is usable in constant expressions and has constant destruction.
History
Date User Action Args
2024-11-23 00:00:00admincreate