Lambda and cast to pointer to void

  • Follow


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
0
Reply Severin 9/20/2010 1:09:54 PM

On 20 Sep., 15:09, Severin Ecker wrote:
>
> 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 =3D []{ printf("lambda\n"); };
>   decltype(&lambda) addr =3D λ =A0// 1
>   void* ptr =3D (void*)addr; =A0 =A0 =A0 =A0 =A0 // 2
>   decltype(lambda) lambda2 =3D *((decltype(&lambda))ptr); =A0// 3
>   lambda2(); =A0 // 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
0
Reply s.gesemann (661) 9/20/2010 4:02:10 PM


1 Replies
375 Views

(page loaded in 0.382 seconds)

Similiar Articles:













7/26/2012 7:27:39 PM


Reply: