<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 - '*this' captured by copy does not preserve constness of '*this' in mutable lambda"
href="https://bugs.llvm.org/show_bug.cgi?id=51524">51524</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>'*this' captured by copy does not preserve constness of '*this' in mutable lambda
</td>
</tr>
<tr>
<th>Product</th>
<td>clang
</td>
</tr>
<tr>
<th>Version</th>
<td>trunk
</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>enhancement
</td>
</tr>
<tr>
<th>Priority</th>
<td>P
</td>
</tr>
<tr>
<th>Component</th>
<td>C++2a
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedclangbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>davveston@gmail.com
</td>
</tr>
<tr>
<th>CC</th>
<td>blitzrakete@gmail.com, erik.pilkington@gmail.com, llvm-bugs@lists.llvm.org, richard-llvm@metafoo.co.uk
</td>
</tr></table>
<p>
<div>
<pre>The following program:
// --------------------
#include <type_traits>
struct S;
void f(S *) = delete; // #1
void f(const S *){}; // #2
struct S {
void g() const {
[*this]() mutable { f(this); }();
}
};
int main() {
S s{};
s.g();
}
// ---------------
is well-formed. As per [expr.prim.lambda.closure]/12 [1]
<span class="quote">> [...] but for purposes of name lookup, **determining the type**
> and value **of this** [...] the compound-statement is considered
> in the context of the lambda-expression. </span >
and [class.this]/1 [2]
<span class="quote">> In the body of a non-static ([class.mfct]) member function, the
> keyword this is a prvalue whose value is a pointer to the object
> for which the function is called. **The type of this** in a member
> function whose type has a cv-qualifier-seq cv and whose class is
> X is “pointer to cv X”.</span >
the type of 'this' in a lambda defined in a const-qualified member function,
even when referred to from in the compound-statement of a lambda, is a
const-qualified type. This also holds even if the lambda is mutable and the
'*this' object is captured by copy. Thus, the 'f(this)' call in the program
above should resolve to overload #2, not overload #1.
However, Clang (for various versions and for {std=c++17, -std=c++2a}) rejects
the program as 'this' when referred to from within the lambda's
compound-statement is considered to be of non-const type, thus picking the
deleted overload #1. GCC and MSVC (various versions) both accept the program,
picking overload #2.
The relevant wording (afaict) is aligned e.g. with CWG 756 [3] but for a
copy-capture of the '*this' object (CWG 756 covers intentionally preserving
constness for variables captured by value, even in mutable lambdas).
[1] <a href="https://timsong-cpp.github.io/cppwp/n4861/expr.prim.lambda.closure#12">https://timsong-cpp.github.io/cppwp/n4861/expr.prim.lambda.closure#12</a>
[2] <a href="https://timsong-cpp.github.io/cppwp/n4861/class.this#1">https://timsong-cpp.github.io/cppwp/n4861/class.this#1</a>
[3] <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#756">http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#756</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>