public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/103712] New: variable is not a constant expression because it is used in its own initializer
@ 2021-12-14 16:23 barry.revzin at gmail dot com
  2021-12-14 16:25 ` [Bug c++/103712] " pinskia at gcc dot gnu.org
  0 siblings, 1 reply; 2+ messages in thread
From: barry.revzin at gmail dot com @ 2021-12-14 16:23 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 103712
           Summary: variable is not a constant expression because it is
                    used in its own initializer
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: barry.revzin at gmail dot com
  Target Milestone: ---

Reduced from StackOverflow (https://stackoverflow.com/q/70342678/2069064):

struct selfref {
    selfref* next = nullptr;
};

struct exec {
    selfref mem = selfref{};

    constexpr exec() {
        mem.next = &mem;
    }
};

constexpr exec do_thing() {
    return exec{};
}


constexpr exec ret = do_thing();
constexpr selfref* ptr = ret.mem.next;

I think this should compile - ret.mem.next points to ret.mem, which is okay
since ret has static storage duration. And then ptr should be pointing to
something that has static storage duration, which is a permitted result.

gcc currently rejects with:

<source>:19:26: error: the value of 'ret' is not usable in a constant
expression
   19 | constexpr selfref* ptr = ret.mem.next;
      |                          ^~~
<source>:18:16: note: 'ret' used in its own initializer
   18 | constexpr exec ret = do_thing();
      |                ^~~

This seems vaguely related to CWG 2278, but do_thing() isn't doing copy
elision, so I think this should work.

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

* [Bug c++/103712] variable is not a constant expression because it is used in its own initializer
  2021-12-14 16:23 [Bug c++/103712] New: variable is not a constant expression because it is used in its own initializer barry.revzin at gmail dot com
@ 2021-12-14 16:25 ` pinskia at gcc dot gnu.org
  0 siblings, 0 replies; 2+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-12-14 16:25 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |DUPLICATE

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Dup of bug 64989.

*** This bug has been marked as a duplicate of bug 64989 ***

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

end of thread, other threads:[~2021-12-14 16:25 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-14 16:23 [Bug c++/103712] New: variable is not a constant expression because it is used in its own initializer barry.revzin at gmail dot com
2021-12-14 16:25 ` [Bug c++/103712] " pinskia at gcc dot gnu.org

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).