https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116518
Bug ID: 116518 Summary: GCC does not optimize-out useless operations. Clang does. Product: gcc Version: 15.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: socketpair at gmail dot com Target Milestone: --- https://godbolt.org/z/d46448vqa ---------------------------- -std=c++23 -O3 -fno-exceptions -------------------------------- #include <algorithm> #include <array> #include <ranges> #include <string> #include <vector> using namespace std; namespace { consteval string gen() { return "xxx"; } class qwe { public: consteval qwe(const string& str) : s(str.size()) { str.copy(x.data(), s); } constexpr operator const vector<char>() const { return {x.cbegin(), x.cbegin() + s}; } private: // 1048576 is a clang limit array<char, 10000> x{}; size_t s; }; } // namespace int fun1() { const vector<char> v2{qwe(gen())}; return v2.size(); } ----------------- fun1(): subq $10024, %rsp movl $10008, %edx xorl %esi, %esi movq %rsp, %rdi call memset movl $3, %edi movl $7895160, (%rsp) call operator new(unsigned long) movzwl (%rsp), %edx movl $3, %esi movq %rax, %rdi movw %dx, (%rax) movzbl 2(%rsp), %edx movb %dl, 2(%rax) call operator delete(void*, unsigned long) movl $3, %eax addq $10024, %rsp ret ----------------- Clang: ---------------- fun1(): mov eax, 3 ret ----------------