[llvm-bugs] [Bug 36750] New: Meyers' singleton does not work with [[gnu::const]] and -O1 to -O3
via llvm-bugs
llvm-bugs at lists.llvm.org
Thu Mar 15 06:52:41 PDT 2018
https://bugs.llvm.org/show_bug.cgi?id=36750
Bug ID: 36750
Summary: Meyers' singleton does not work with [[gnu::const]]
and -O1 to -O3
Product: clang
Version: 5.0
Hardware: PC
OS: Linux
Status: NEW
Severity: normal
Priority: P
Component: -New Bugs
Assignee: unassignedclangbugs at nondot.org
Reporter: antons.jelkins at bmw.de
CC: llvm-bugs at lists.llvm.org
If Mayers’ singleton is defined with [[gnu::const]] attribute, the class
constructor is not called and the singleton getter returns an uninitialised
object.
Reproduction:
$ cat main.cpp
#include <iostream>
class MyClass
{
public:
MyClass() : m_val(42) {}
void handle() const { std::cout << m_val << std::endl; }
private:
int m_val;
};
[[ gnu::const ]]
static const MyClass &myClass() noexcept
{
static const MyClass s_myClass;
return s_myClass;
}
int main()
{
myClass().handle();
return 0;
}
$ clang++ -O0 -std=c++14 ./main.cpp && ./a.out
42
$ clang++ -O1 -std=c++14 ./main.cpp && ./a.out
0
Expectation:
In both cases 42 is printed.
Observation:
In the second case the MyClass object is not constructed. This assembly code is
generated by clang:
main: # @main
push rax
mov edi, offset myClass()::s_myClass
call MyClass::handle() const
xor eax, eax
pop rcx
ret
--
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20180315/f0f02992/attachment.html>
More information about the llvm-bugs
mailing list