<html>
<head>
<base href="https://bugs.llvm.org/">
</head>
<body><table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Bug ID</th>
<td><a class="bz_bug_link
bz_status_NEW "
title="NEW - Meyers' singleton does not work with [[gnu::const]] and -O1 to -O3"
href="https://bugs.llvm.org/show_bug.cgi?id=36750">36750</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>Meyers' singleton does not work with [[gnu::const]] and -O1 to -O3
</td>
</tr>
<tr>
<th>Product</th>
<td>clang
</td>
</tr>
<tr>
<th>Version</th>
<td>5.0
</td>
</tr>
<tr>
<th>Hardware</th>
<td>PC
</td>
</tr>
<tr>
<th>OS</th>
<td>Linux
</td>
</tr>
<tr>
<th>Status</th>
<td>NEW
</td>
</tr>
<tr>
<th>Severity</th>
<td>normal
</td>
</tr>
<tr>
<th>Priority</th>
<td>P
</td>
</tr>
<tr>
<th>Component</th>
<td>-New Bugs
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedclangbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>antons.jelkins@bmw.de
</td>
</tr>
<tr>
<th>CC</th>
<td>llvm-bugs@lists.llvm.org
</td>
</tr></table>
<p>
<div>
<pre>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</pre>
</div>
</p>
<hr>
<span>You are receiving this mail because:</span>
<ul>
<li>You are on the CC list for the bug.</li>
</ul>
</body>
</html>