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

            Bug ID: 105737
           Summary: Incorrect evaluation order in new expression
           Product: gcc
           Version: 11.3.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: m.cencora at gmail dot com
  Target Milestone: ---

Following program when compiled with -std=c++17 prints 1243, while it should
print 1234.
All gcc < 12 versions are affected.

#include <cstdio>

struct MyStruct
{
};

struct MyTuple
{
    int a;
    int b;
    MyStruct c;
    int d;
};

int deserializeInt(const char*s) noexcept
{
    std::puts(s);
    return 0;
}

MyStruct deserializeStruct(const char*s) noexcept
{
    std::puts(s);
    return {};
}


int main()
{
#if 0 // this disabled version works fine
    MyTuple a = {
        deserializeInt("1"),
        deserializeInt("2"),
        deserializeStruct("3"),
        deserializeInt("4")
    };
#else
    new MyTuple{
        deserializeInt("1"),
        deserializeInt("2"),
        deserializeStruct("3"),
        deserializeInt("4")
    };
#endif
}

Similar bug exists when placement new is used.
While this problem seems to be fixed in gcc 12, I have not found a bug report
for such a fix, so it may have been fixed by accident, so it would be good to
at least have a test case that this doesn't regress in future.
But the best would be get this fixed in gcc9, 10 and 11.

Reply via email to