Different sizes of lambda expressions in VS2010?

Out of curiosity, I checked the size of the lamba expression. My first thought was that they would be 4big bytes, like a pointer to a function. Oddly enough, the output of my first test was 1:

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

Then I checked with some calculations inside the lambda, with the output 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 was random, but the result finally changed, displaying the expected 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. The ideal still displays 1as a result.
I know the standard rule that a lambda expression cannot appear in an invaluable context, but an afyka that takes into account only the direct use of a lambda, for example, sizeof([&]() -> void {std::cout << "Forbidden." << std::endl;})
on which VS2010 tells me a compiler error. Has anyone understood what is happening?

+3
source share
2 answers

@Hans Passant . , , , , , , .
4 ( ).

+2

Visual Studio, , - . , . , . , , ... .

+1

Source: https://habr.com/ru/post/1794223/


All Articles