https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78773
Bug ID: 78773 Summary: "Empty" const only class not optimized away Product: gcc Version: 7.0 Status: UNCONFIRMED Keywords: missed-optimization Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: jpakkane at gmail dot com Target Milestone: --- Suppose we have the following very simple class for accessing a memory mapped register: class MemoryMappedRegister { public: MemoryMappedRegister(const int value) : ptr{reinterpret_cast<char*>(value)} {} void write(char v) const { *ptr = v; } private: volatile char* const ptr; }; const MemoryMappedRegister MyReg(12345); void set_value(char v) { MyReg.write(v); } That is, the only reason for this class to exist is to provide a nicer UI to register poking than C macros. When compiled with GCC (up to the version of 7 in gcc.godbolt.org as of this writing) using -O2 -std=c++14 the output is this: set_value(char): mov rax, QWORD PTR MyReg[rip] mov BYTE PTR [rax], dil ret mov QWORD PTR MyReg[rip], 12345 ret That is, it reserves space for the object to store the always constant pointer value and loads it indirectly. When compiled with Clang 3.9 the output is this: set_value(char): # @set_value(char) mov byte ptr [12345], dil ret Here no space is reserved for the object and the write is using immediate mode.