[Bug c++/95785] New: Compiler rejects instantiation of a class using constexpr new/delete in its constructor/destructor

poggenhans at msn dot com gcc-bugzilla@gcc.gnu.org
Sat Jun 20 12:16:27 GMT 2020


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95785

            Bug ID: 95785
           Summary: Compiler rejects instantiation of a class using
                    constexpr new/delete in its constructor/destructor
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: poggenhans at msn dot com
  Target Milestone: ---

Consider the following example (life example: https://godbolt.org/z/WwafNZ):


class NewDelete {
 public:
  constexpr NewDelete() {
    data_ = new int();
    *data_ = 0;
  }

  constexpr ~NewDelete() { delete data_; }

  constexpr int get() const { return *data_; }

 private:
  int* data_{};
};

void compiles() { static_assert(NewDelete().get() == 0); }
void doesNotCompile() { constexpr NewDelete nd; }


GCC 10.1 and above fails to compile "doesNotCompile()" in C++20 mode. However
the slightly more complex function "compiles()" is compiled without problems.


Here is the error:
<source>: In function 'void doesNotCompile()':
<source>:5:21: error: 'NewDelete()' is not a constant expression because it
refers to a result of 'operator new'
    5 |     data_ = new int();
      |                     ^


More information about the Gcc-bugs mailing list