Lambda and cast to pointer to void

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
 
S

SG

I was wondering whether the following piece of code is legal.

Not yet. But I'm assuming "experimental C++0x mode" from now on.
:)
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?

Yes. 'lambda' is an lvalue which refers to some object with automatic
storage.
// 2) is it (then) legal to cast the pointer to the lambda to a pointer
to void

Yes. But the "(void*)" is not necessary. This conversion is implicit.
// 3) is it then legal (and most importantly well defined) to cast the
void* back to the original closure type

Sure. The roundtrip T* --> void* --> T* is guaranteed to be lossless
in case T is an "object type". (I'm assuming that the first convertion
is done implicitly and the second one is done with a static_cast.)

In your example you use C-style casts which -- in this case -- should
be equivalent to static casts.

You actually make a copy of the lambda object.
// 4) and finally is it legal to invoke the "back-cast" lambda.

There's no casting at line 4. You simply invoke the function call
operator on the copied lambda object.

Cheers!
SG
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top