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.
  • [Bug c++/103712] New: variable ... barry.revzin at gmail dot com via Gcc-bugs

Reply via email to