https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116416
Bug ID: 116416 Summary: Missing optimization: compile time evaluation of prvalue Product: gcc Version: 14.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: nuttyrunner at gmail dot com Target Milestone: --- gcc -x c++ -Og -std=c++20 yields inconsistent compile time evaluation for following example: struct Str { constexpr Str(){ } constexpr Str(const char *instr) { str = instr; length = 0; for(auto index = 0;instr[index];++index) { ++length; } } const char *str = nullptr; int length = 0; }; extern void callback(Str str); void func1() { callback(Str{"Test"}); // Does not get constexpr optimized by gcc } void func2() { Str str{"Test"}; callback(str); // Does get constexpr optimized by gcc } func2's lvalue str initializer gets compile time evaluated and the call yields plain mov-s in assembly, but prvalue Str{"Test"} does not - instead it inlines the constructor and performs the counting each time func1 is invoked. constexpr does not guarantee compile time evaluation, however this specific usecase is extremely important to our code base for not counting the lengths of strings each time performance-wise, and it seems to be some small semantic difference? Please note that the "callback" function call is not important, same happens without it, it is in the example to illustrate that it is unrelated to dead code removal. That optimization is something we could try and look into in the gcc codebase to propose a patch, but we would appreciate any advice/pointers as to where to look. Godbolt: https://godbolt.org/z/W1KK3hqao