[llvm-bugs] [Bug 51524] New: '*this' captured by copy does not preserve constness of '*this' in mutable lambda
via llvm-bugs
llvm-bugs at lists.llvm.org
Wed Aug 18 08:48:16 PDT 2021
https://bugs.llvm.org/show_bug.cgi?id=51524
Bug ID: 51524
Summary: '*this' captured by copy does not preserve constness
of '*this' in mutable lambda
Product: clang
Version: trunk
Hardware: PC
OS: Linux
Status: NEW
Severity: enhancement
Priority: P
Component: C++2a
Assignee: unassignedclangbugs at nondot.org
Reporter: davveston at gmail.com
CC: blitzrakete at gmail.com, erik.pilkington at gmail.com,
llvm-bugs at lists.llvm.org, richard-llvm at metafoo.co.uk
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]
> [...] 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.
and [class.this]/1 [2]
> 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”.
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] https://timsong-cpp.github.io/cppwp/n4861/expr.prim.lambda.closure#12
[2] https://timsong-cpp.github.io/cppwp/n4861/class.this#1
[3] http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#756
--
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/20210818/890092d5/attachment.html>
More information about the llvm-bugs
mailing list