I was reading Scott Meyer's Effective Modern C++ and hit the item in which he's suggesting usage of lambda
s in place of std::function
and std::bind
. I understand his argument and his claim about drawbacks of std::function
and I agreed with him.
As of today, I decided to switch to templates for storing lambda
s (which do not need mutators). I do understand that type of every lambda
is only known to the compiler and even two identical lambda
s will have different types so how come the following code compiles and works just fine?
template<typename LambdaT>
class CaptureLambda
{
public:
CaptureLambda(const LambdaT& fn)
: mActionFn(fn) // initialize mActionFn to a user supplied lambda
{}
private:
LambdaT mActionFn{ []{} }; // initialize mActionFn to an empty lambda
};
My point of confusion is that, how come mActionFn
is default initiated to an empty lambda with a different type inside member declarations but the constructor of the class is happily accepting another type of lambda
in its arguments? are they cast
able to each other? if yes, why the following makes the compiler sad?
// Class stuff...
template<typename T>
void resetActionFn(const T& newFn) { // setter member
mActionFn = newFn;
}
// Class stuff...
Aucun commentaire:
Enregistrer un commentaire