public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/95785] New: Compiler rejects instantiation of a class using constexpr new/delete in its constructor/destructor
@ 2020-06-20 12:16 poggenhans at msn dot com
  2023-04-02  3:00 ` [Bug c++/95785] " whitesmith137 at outlook dot com
  0 siblings, 1 reply; 2+ messages in thread
From: poggenhans at msn dot com @ 2020-06-20 12:16 UTC (permalink / raw)
  To: gcc-bugs

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();
      |                     ^

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [Bug c++/95785] Compiler rejects instantiation of a class using constexpr new/delete in its constructor/destructor
  2020-06-20 12:16 [Bug c++/95785] New: Compiler rejects instantiation of a class using constexpr new/delete in its constructor/destructor poggenhans at msn dot com
@ 2023-04-02  3:00 ` whitesmith137 at outlook dot com
  0 siblings, 0 replies; 2+ messages in thread
From: whitesmith137 at outlook dot com @ 2023-04-02  3:00 UTC (permalink / raw)
  To: gcc-bugs

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

Tyler Smith <whitesmith137 at outlook dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |whitesmith137 at outlook dot com

--- Comment #1 from Tyler Smith <whitesmith137 at outlook dot com> ---
Not a bug.

The full-expression of the init-declarator `nd` ([intro.execution]/5.4) must be
a constant expression ([dcl.constexpr]/10/3). This "implicit" expression
comprises the prvalue `NewDelete()` (which is the expression that the compiler
is complaining about). As such, it must satisfy the constraints under
[expr.const]/11. Specifically, as a class type, each subobject (i.e. `data_`)
must also satisfy these constraints (11.4); then, as a pointer type, `data_`
must only address either a static-storage object, non-immediate function, or be
null (11.2). Since it instead points to dynamic storage from `new int()`,
`NewDelete()` is not a constant expression.

Furthermore, this full-expression is not even a _core_ constant expression
because its evaluation does not include deallocation of the storage obtained
via `new int()` ([expr.const]/5.17).

The full-expression within the `static_assert` is a valid constant expression
because its evaluation _does_ include the deallocation via `delete data_`.
Here, `NewDelete()` materializes a temporary which is then destroyed at the end
of the full-expression, invoking the destructor doing the deallocation, all as
part of the same evaluation.

(The above references are to the N4861 C++20 draft.)

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-04-02  3:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-20 12:16 [Bug c++/95785] New: Compiler rejects instantiation of a class using constexpr new/delete in its constructor/destructor poggenhans at msn dot com
2023-04-02  3:00 ` [Bug c++/95785] " whitesmith137 at outlook dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).