Re: Rationale for lambda-expressions not being allowed in unevaluated contexts
On 23 Nov., 08:18, Matt Calabrese wrote:
In the current working paper at 5.1.2 p2 I notice that it is specified
that "a lambda-expression shall not appear in an unevaluated operand".
I was wondering what the rationale is for this?
I ask because I have a
use-case:
////////////////////
///// Implementation
#define AUTO_FUNCTION( ... )\
auto __VA_ARGS__ -> AUTO_FUNCTION_IMPL
#define AUTO_FUNCTION_IMPL( ... )\
decltype( [&]{ __VA_ARGS__ }() ) { __VA_ARGS__ }
///// Usage
template< class L, class R >
AUTO_FUNCTION( add( L left, R right ) )
(
return left + right;
)
////////////////////
Read on (paragraph 3):
"The type of the lambda-expression (which is also the type of the
closure object) is a unique, unnamed nonunion class type..."
So, at least the use of decltype makes little sense because two lambda
expression never have the same type.
It seems, what you're trying to do is to remove the redundancy we're
currently stuck with (unfortunately) with respect to the '->'-syntax
for the late return type. But I don't see how this has anything to do
with lambda expressions. Try this:
#define AUTOFUN_IMPL(...) -> decltype(__VAR_ARGS__) \
{return __VAR_ARGS__;}
template<class L, class R>
auto add(L left, R right)
AUTOFUN_IMPL(left+right)
In my opinion, C++0x should treat "auto functions" with a lacking "->
type" the same way it handles lambda expressions already. The function
could be implicitly inline and needs to have a single statement of the
form "return expression;" as its body. However, as far as I know, the
"-> type" part is not optional for non-lambdas. :-(
In other cases you might be able to use 'auto' like this:
auto fun = [](int x, int y){return x>y;}
std::set<int,decltype(fun)> myset (fun);
Here, decltype is not applied on a lambda expression but an object of
a corresponding closure type which is fine.
Cheers,
SG
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]