Code:
auto main() -> int
{
int b = 42;
auto lambasta = [&]()
{
using B_type = decltype( b );
return B_type{};
};
(void) lambasta;
}
Compiles with no diagnostic with MinGW g++ 6.3.0 -std=c++14 -Wall -pedantic-errors
. Fails to compile with Visual C++ 2015 update 3,
foo.cpp(6): error C2065: 'b': undeclared identifier
A type lambda lets one express a higher-kinded type directly, without a type definition. For instance, the type above defines a binary type constructor, which maps arguments X and Y to Map[Y, X] . Type parameters of type lambdas can have bounds, but they cannot carry + or - variance annotations.
Much like functions can change the value of arguments passed by reference, we can also capture variables by reference to allow our lambda to affect the value of the argument. To capture a variable by reference, we prepend an ampersand ( & ) to the variable name in the capture.
C++ Lambda expression allows us to define anonymous function objects (functors) which can either be used inline or passed as an argument. Lambda expression was introduced in C++11 for creating anonymous functors in a more convenient and concise way.
The capture list defines the outside variables that are accessible from within the lambda function body. The only capture defaults are. & (implicitly capture the used automatic variables by reference) and. = (implicitly capture the used automatic variables by copy).
Possible workaround:
template<typename T>
struct wrapper
{
using wrapped_t = T;
};
auto main() -> int
{
int b = 42;
auto lambasta = [&, a = wrapper<decltype(b)>()]()
{
using B_type = typename decltype( a ) ::wrapped_t;
return B_type{};
};
(void) lambasta;
}
Works on GCC 6.3 and MSVC 2015 up 3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With