[Bug c++/84099] New: Dynamic initialization is performed in case when constant initialization is permitted

antoshkka at gmail dot com gcc-bugzilla@gcc.gnu.org
Mon Jan 29 12:45:00 GMT 2018


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

            Bug ID: 84099
           Summary: Dynamic initialization is performed in case when
                    constant initialization is permitted
           Product: gcc
           Version: 8.0.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: antoshkka at gmail dot com
  Target Milestone: ---

The following code 

struct foo {
    const char* data_;
    unsigned size_;

    foo(const char* data, unsigned size) noexcept
        : data_(data)
        , size_(size)
    {}
};

foo test() {
    static const foo v{"Hello", 5};
    return v;
}


Produces disassembly with dynamic initialization of the `v` variable. However
in this case C++ Standard permits constant initialization:

"An implementation is permitted to perform the initialization of a variable
with static or thread storage duration as a static initialization even if such
initialization is not required to be done statically, provided that

— the dynamic version of the initialization does not change the value of any
other object of static or thread storage duration prior to its initialization,
and
— the static version of the initialization produces the same value in the
initialized variable as would be produced by the dynamic initialization if all
variables not required to be initialized statically were initialized
dynamically.
"

Optimal assembly would look like

.LC0:
  .string "Hello"
test():
  mov eax, OFFSET FLAT:.LC0
  mov edx, 5
  ret


More information about the Gcc-bugs mailing list