As I know all data types must be known at compile time, and lambda is not a type. Does lambda got translated into anonymous struct with operator()
or std::function
wrapped?
For example,
std::for_each(v.begin(), v.end(), [](int n&){n++;});
As I know all data types must be known at compile time, and lambda is not a type. Does lambda got translated into anonymous struct with operator()
or std::function
wrapped?
For example,
std::for_each(v.begin(), v.end(), [](int n&){n++;});
A variation of the as-if rule, the C++11 standard says:
I believe this is what people mean when they say that it's unspecified. However what's guaranteed as already stated in the other answers is the following:
Original author: Lightness Races in Orbit
A lambda expression constructs an unnamed type, with each one having a different type. They are not
std::function
implementations. More info is provided here: What is a lambda expression in C++11? and here: How to convert a lambda to an std::function using templatesYou can unveil the type on your specific compiler with a trick like this:
Compiling with clang on my mac gives:
@Barry points out that you can use
typeid
instead. If I print outtypeid(a).name()
andtypeid(b).name()
on my system, I get:which demangle to
Just wanted to include this for completeness. I actually find the error message version a little more informative. :)
From the standard §5.1.2.3:
It is its own type. Every time. So for instance:
a
andb
will necessarily have different types. They are both convertible tostd::function<int()>
, but not to each other:Adding a few more examples to add some clarity: