c++ - Different sizes of lambda expressions in VS2010? -


out of curiosity, tested size of lamba expression. first thought was, they'd 4 bytes big, function pointer. strangely, output of first test 1:

auto my_lambda = [&]() -> void {}; std::cout << sizeof(my_lambda) << std::endl; 

then tested calculations inside lambda, output still being 1:

auto my_lambda2 = [&]() -> void {int i=5, j=23; std::cout << i*j << std::endl;}; std::cout << sizeof(my_lambda2) << std::endl; 

my next idea kinda random, output changed, displaying awaited 4:

auto my_lambda3 = [&]() -> void {std::cout << sizeof(my_lambda2) << std::endl;}; std::cout << sizeof(my_lambda3) << std::endl; 

at least in visual studio 2010. ideone still display 1 output.
know of standard rule, lambda expression cannot appear in unevaluated context, afaik counts direct lambda use like
sizeof([&]() -> void {std::cout << "forbidden." << std::endl;})
on vs2010 prompts me compiler error. got idea what's going on?

thanks @hans passant's comment under question, solution found. original approach wrong in fact thought every object captured lambda, isn't case, in enclosing scope are, , if used.
, of captured objects, 4 bytes used (size of reference).


Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -