|
Boost : |
From: Gabriel Dos Reis (gdr_at_[hidden])
Date: 2004-03-07 23:27:13
Jaakko Jarvi <jajarvi_at_[hidden]> writes:
| My example was a bit contrived too.
| Here's another example (with imaginary syntax):
|
| auto foo() {
| int i;
| return auto(int x) { return i + x; }
| }
Is it different from
int& f() { int i; return i; }
?
In -a- model I discussed a while ago with the second author of
decltype/auto proposal, lambdas are unnamed constants that can be
implemented by local classes. The body of the lambda would be
forwarded-to by the appropriate overload of the call operator,
i.e. your example would be
foo: () -> auto {
int i;
struct xxx {
operator(): (int x) -> auto { return i + x; }
};
};
That is a very simple model that just provides a syntactic sugar
(modulo use of auto in return type) for what you can do in current
C++. In particular, it is clear that the reference to "i" is a hard
error by application of current rules governing local classes.
I do not believe in local variables that silently escape their lexical
scopes. If you really want your automatic variables to espace, you
already have ways to say so; but I don't think they should be the
common cases that should be "optimized for".
Note that I want the lambdas to be constants, in particular I would
like to be able to use them as template arguments. Note also the type
inference that can refer to local classes :-)
-- Gaby
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk