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

            Bug ID: 96972
           Summary: Missed constant propagation for forward declared
                    constexpr in composite class
           Product: gcc
           Version: 10.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: deve...@manuel-voegele.de
  Target Milestone: ---

Created attachment 49196
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=49196&action=edit
Preprocessed source code to reproduce the bug

The attached source code contains class A that contains a single instance of
class B. The value of the only field of class B is initialized with a value
calculated by a constexpr function. The whole code could be optimized away and
replaced by constant value.

However when compiling for x64 using `g++ -O3 -o main main.cpp` gcc will
instead emit a loop that calculates that value at runtime.

This bug happens only in this specific constellation of classes. Doing one of
the following will cause gdb to correctly replace that code with a constant:
- Implementing buildnumber in the class body instead of forward declaring it
and implementing it outside the class body
- Removing the explicitly declared constructor of class B, using the default
constructor generated by the compiler
- Directly instantiating A in the main function instead of instantiating B.

Reply via email to