std::function
copies its arguments. Since the object you want to assign is of the base type (and has a pure virtual member function), it cannot be copied. Note that, if it didn't have a pure virtual member function, it might be copyable, but you would suffer from object slicing.
Using std::ref
is safe as long as you make sure that the object to which std::ref
is bound lives longer that all references to it.
The most elegant solution, in my opinion, would be to make assignFunctorPtr
a function-template that takes an argument of the real type of functor (as opposed to a base-type). If this is copyable, the assignment would work without std::ref
.
template<class SlotType>
void assignFunctorPtr(SlotType* slot_ptr)
{
f = *slot_ptr; // works if SlotType is copyable
}
I believe this version would also work if SlotType
was just movable, but I might be wrong there.