Re: lambdas and non-captured, unevaluated operands
On 23 Apr., 15:16, Paul Bibbings <paul.bibbi...@gmail.com> wrote:
From the above, my reading is that g5 is OK since the presence of i does
not, in this context, constitute a 'use'.
Exactly.
Thus, from [basic.def.odr],
?3.2/2:
"An expression is potentially evaluated unless it is an unevaluated
operand (Clause 5) or a subexpression thereof. A variable or
non-overloaded function whose name appears as a potentially-evaluated
expression is used ..."
and, of course, this is the case for the operand to sizeof in the
example above:
[expr.sizeof] ?5.3.3/1:
"The sizeof operator yields the number of bytes in the object
representation of its operand. The operand is either an expression,
which is an unevaluated operand (Clause 5), or a parenthesized
type-id."
What appears to be missing in [expr.prim.lambda] (unless it is me that
is missing something) is some statement indicating how it happens - in
the absence of explicit or implicit capture - that i is nevertheless
`available' in the context of the compound-statement of the
lambda-expression in g5, above; in effect, by what mechanism it is
available outside of being captured.
What you are missing is [expr.prim.lambda]/7:
"The lambda-expression's compound-statement yields the function-body
(8.4)
of the function call operator, but for purposes of name lookup (3.4),
[..], the compound-statement is considered in the context of the
lambda-
expression."
So this defines the name lookup of any entity named in the
lambda-expression. It means that the name 'i' within g5 is
resolved in the same way as this name would be resolved
at the textual position where the lambda expression occurred,
i.e.
void f2() {
int i = 1;
// ...
void g5(int = sizeof i);
}
This is also well-formed, because i is not used, since it appears
in an unevaluated operand (I'm aware that several compilers don't
get that right).
I am wanting to get a better sense of this since gcc-4.5.0 does not
accept the above code, failing it with:
11:53:03 Paul Bibbings@JIJOU
/cygdrive/d/CPPProjects/nano/gcc_bugs $cat _5_1_2_13.cpp
// file: _5_1_2_13.cpp
void f2() {
int i = 1;
// ...
void g5(int = ([]{ return sizeof i; })());
}
11:53:06 Paul Bibbings@JIJOU
/cygdrive/d/CPPProjects/nano/gcc_bugs $i686-pc-cygwin-gcc-4.5.0 -Wall
-Wno-unused -std=c++0x -pedantic -c _5_1_2_13.cpp
_5_1_2_13.cpp: In lambda function:
_5_1_2_13.cpp:6:37: error: local variable ?i? may not appear in this
context
_5_1_2_13.cpp:6:40: error: return-statement with a value, in function
returning 'void'
_5_1_2_13.cpp: In function ?void f2()?:
_5_1_2_13.cpp:6:44: error: default argument for ?int <anonymous>? has
type ?void?
This is a compiler bug.
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]