S
Severin Ecker
Hi,
I was wondering whether the following piece of code is legal. I've had a
rather quick look at section 5.1.2 of the current working draft, but
there might be important sections of the standard regarding this code
somewhere else, so I'd be happy for pointers to the right sections.
(g++ 4.5.1 and vs2010 compile it fine and the executable does what it's
intended to do, but that doesn't tell me all too much about its legality
auto lambda = []{ printf("lambda\n"); };
decltype(&lambda) addr = λ // 1
void* ptr = (void*)addr; // 2
decltype(lambda) lambda2 = *((decltype(&lambda))ptr); // 3
lambda2(); // 4
so (apart from probably obvious errors; since I'm rather new in
experimenting with the new auto/decltype/lambda features) a few
questions arise:
// 1) is it legal to take the address of a lambda?
// 2) is it (then) legal to cast the pointer to the lambda to a pointer
to void (reading 5.1.2/3 tells me that the type of a lambda expression
if of "class type", so I would say that the lambda itself is an object
of said closure type, and according to 3.9.2/4 a void* shall be able to
hold any object pointer
// 3) is it then legal (and most importantly well defined) to cast the
void* back to the original closure type
// 4) and finally is it legal to invoke the "back-cast" lambda.
many thanks in advance!
cheers,
severin
I was wondering whether the following piece of code is legal. I've had a
rather quick look at section 5.1.2 of the current working draft, but
there might be important sections of the standard regarding this code
somewhere else, so I'd be happy for pointers to the right sections.
(g++ 4.5.1 and vs2010 compile it fine and the executable does what it's
intended to do, but that doesn't tell me all too much about its legality
auto lambda = []{ printf("lambda\n"); };
decltype(&lambda) addr = λ // 1
void* ptr = (void*)addr; // 2
decltype(lambda) lambda2 = *((decltype(&lambda))ptr); // 3
lambda2(); // 4
so (apart from probably obvious errors; since I'm rather new in
experimenting with the new auto/decltype/lambda features) a few
questions arise:
// 1) is it legal to take the address of a lambda?
// 2) is it (then) legal to cast the pointer to the lambda to a pointer
to void (reading 5.1.2/3 tells me that the type of a lambda expression
if of "class type", so I would say that the lambda itself is an object
of said closure type, and according to 3.9.2/4 a void* shall be able to
hold any object pointer
// 3) is it then legal (and most importantly well defined) to cast the
void* back to the original closure type
// 4) and finally is it legal to invoke the "back-cast" lambda.
many thanks in advance!
cheers,
severin