C++ Logo

std-discussion

Advanced search

Capturing non-const unnamed data members coming from captures by-copy

From: mauro russo <ing.russomauro_at_[hidden]>
Date: Thu, 9 Oct 2025 13:06:53 +0200
In the current text of the standard, C++23, as well as
on https://eel.is/c++draft/expr.prim.lambda,

I do see a potential imprecision related to the introduction
of the explicit object parameter.

In particular,
[expr.prim.lambda.closure]-p7 reads:
"
...
Otherwise, it is a non-static member function or member function template (
[class.mfct.non.static] <https://eel.is/c++draft/class.mfct.non.static>)
that is declared const ([class.mfct.non.static]
<https://eel.is/c++draft/class.mfct.non.static>) if and only if the
*lambda-expression*
<https://eel.is/c++draft/expr.prim.lambda#nt:lambda-expression>'s
*parameter-declaration-clause*
<https://eel.is/c++draft/dcl.fct#nt:parameter-declaration-clause> is not
followed by mutable and the *lambda-declarator*
<https://eel.is/c++draft/expr.prim.lambda#nt:lambda-declarator> does not
contain an explicit object parameter.
<https://eel.is/c++draft/expr.prim.lambda#closure-7.sentence-2>
 ...
"

so that the unnamed non-static data member is const if and only if
either there is mutable or there is an explicit object parameter.

However,
[expr.prim.lambda.capture]-p(14.1) related
to capturing in nested lambda something
captured by the surrounding lambda, reads:
"
If m1 captures the entity by copy, m2 captures the corresponding non-static
data member of m1's closure type; if m1 is not mutable, the non-static data
member is considered to be const-qualified.
<https://eel.is/c++draft/expr.prim.lambda#capture-14.1.sentence-1>
"

It seems the final part highlighting only the case of mutable is incomplete,
and I guess it was intended to cover also the case of explicit object
parameter.
I also verified here: https://godbolt.org/z/83vnEnMKx
that both gcc and clung do so.

Maybe a simple solution is just to cut that final part of (14.1), because
the text of [expr.prim.lambda.closure]-p7 reported above is already clear
on the fact that the data member are non-const in those two cases.

Am I missing any other motivation that makes the note still needed ?
Is such a motivation really applicable only to the mutable case ?

Otherwise, is it better to keep the note by focusing it on the
const-qualification only (no reference to 'mutable') but indicate
to 'see above' to know when the const is applied ?


Regards,
Mauro.

Received on 2025-10-09 11:07:10