You can have the best of both worlds
template
class MsgProcessorT:public IMsgProcessor{
F f_;
public:
MsgProcessorT(F f):f_(f){}
virtual void handle_msg(const Message& msg) {
f_(msg);
}
};
template
IMsgProcessor* CreateMessageProcessor(F f){
return new MsgProcessor(f);
};
Then you can either use like this
Socket s(CreateMessageProcessor([](const Message& msg){...}));
Or to make it even easier add another constructor to Socket
class Socket{
...
template
Socket(F f):processor_(CreateMessageProcessor(f){}
};
Then you could do
Socket s([](const Message& msg){...});
And still have the same efficiency as a virtual function call