Exercise 2. Lambdas from scratch.

Exercise 2a. Sizeof lambda.

Open this wandbox (backup).

1. The main routine contains 16 test cases in 8 sets. Before you run each set, decide what you think the output is going to be. What is the expected sizeof each lambda? What is the expected return value? Does the return value change if you call it a second time?

2. Notice that none of the lambdas have size zero. This is because no C++ object is allowed to have size zero. Modify the program so that test prints out "empty" for types where std::is_empty_v<F> and "not empty" for other types. Run each of the 16 test cases again. Do the results make sense?

(The variable template std::is_empty_v<F> is declared in <type_traits>. If you're using a pre-C++17 compiler, try std::is_empty<F>::value instead.)

3. Change the definition of test so that it is no longer a template, but simply takes a single parameter of type std::function<int()>. What output do you expect now? Do you expect the output to be different between Clang and GCC? Try it. Maybe try the same code on Rextester.


You're done with the second set of exercises! Sit back and relax, or optionally, browse the following blog posts.