<html>
<head>
<base href="http://llvm.org/bugs/" />
</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 --- - Brace-init of non-const references produces code that crashes"
href="http://llvm.org/bugs/show_bug.cgi?id=20844">20844</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>Brace-init of non-const references produces code that crashes
</td>
</tr>
<tr>
<th>Product</th>
<td>clang
</td>
</tr>
<tr>
<th>Version</th>
<td>3.4
</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>C++11
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedclangbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>petar335335@gmail.com
</td>
</tr>
<tr>
<th>CC</th>
<td>dgregor@apple.com, llvmbugs@cs.uiuc.edu
</td>
</tr>
<tr>
<th>Classification</th>
<td>Unclassified
</td>
</tr></table>
<p>
<div>
<pre>Created <span class=""><a href="attachment.cgi?id=12983" name="attach_12983" title="the program used to reproduce the bug">attachment 12983</a> <a href="attachment.cgi?id=12983&action=edit" title="the program used to reproduce the bug">[details]</a></span>
the program used to reproduce the bug
The attached program produces the following and crashes on Ubuntu LTS 14.04
AMD64:
-----------------------------------------------------------------
random value
65
Segmentation Fault
-----------------------------------------------------------------
If I remove the brace-init in the X's initializer list and replace it with
()-init, everything seems to work as expected.
Additionally, X x2{s()} produces the correct result even with brace-init.
Compiled with no warnings or errors:
clang++ brace_ref_init.cpp -o brace_ref_init -std=c++11
On gcc:
g++ brace_ref_init.cpp -o brace_ref_init -std=c++11
gcc doesn't compile it, saying:
-----------------------------------------------------------------
brace_ref_init.cpp: In instantiation of ‘X::X(T&) [with T = S]’:
brace_ref_init.cpp:39:17: required from here
brace_ref_init.cpp:27:18: error: invalid initialization of non-const reference
of type ‘D&’ from an rvalue of type ‘<brace-enclosed initializer list>’
X(T& v) : d{v} {} // brace-init of d causes incorect behaviour
^
brace_ref_init.cpp: In instantiation of ‘X::X(T&) [with T = D]’:
brace_ref_init.cpp:40:19: required from here
brace_ref_init.cpp:27:18: error: invalid initialization of non-const reference
of type ‘D&’ from an rvalue of type ‘<brace-enclosed initializer list>’
-----------------------------------------------------------------
GCC bug report that I think is related:
<a href="https://gcc.gnu.org/bugzilla/show_bug.cgi?id=50025">https://gcc.gnu.org/bugzilla/show_bug.cgi?id=50025</a></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>