Suppose I have a type my_struct
enclosing a member variable, f
, which is a function. It's possible for f
to be a c++11 lambda function.
Since it is illegal to assign to lambda objects, I'd like to implement my_struct
's assignment operator in such a way that when f
is a lambda, it is not assigned.
Is it possible to build a type trait is_lambda
which can inspect a type for lambda-ness?
In code:
#include <type_traits>
template<typename Function> struct is_lambda
{
// what goes here?
};
template<typename Function> struct my_struct
{
Function f;
my_struct &do_assign(const my_struct &other, std::true_type)
{
// don't assign to f
return *this;
}
my_struct &do_assign(const my_struct &other, std::false_type)
{
// do assign to f
f = other.f;
return *this;
}
my_struct &operator=(const my_struct &other)
{
return do_assign(other, typename is_lambda<Function>::type());
}
};
The bootstrap method information is needed for JVM to construct object representation of lambda during runtime. Lambda body code is generated within an instance or static private method which has the same parameters and return type as lambda's functional interface abstract method.
All lambdas are inline. Not all calls to them are necessarily inlined.
Only A and B are valid.C is invalid because the lambda expression (Apple a) -> a. getWeight() has the signature (Apple) -> Integer, which is different than the signature of the method test defined in Predicate : (Apple) -> boolean.
Short answer: no.
Impossible without compiler support, as the type of a lambda is just a normal, non-union class type.
§5.1.2 [expr.prim.lambda] p3
The type of the lambda-expression (which is also the type of the closure object) is a unique, unnamed nonunion class type [...]
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