I've seen a C++11 library that receives video packets through network and in order to decide what to do with these packets it uses a lambda function like this:
viedoTrack->addDelegate(std::make_shared<FrameWriterInterfaceHelper>([this](const Frame::Ptr &frame) {
//...
Where Frame::Ptr is just shared_ptr<Frame>. I don't like the idea of receiving a reference to a shared_ptr. For me, shared_ptr's should always be passed as copies, so the lambda function owns a copy and if it takes too much time with it, it's guaranteed that the pointer owned by this shared_ptr will not die.
However I interpret the act of passing the shared_ptr as reference as this: "take this pointer, I do not want to own this anymore, you're responsible for it". So if the class that calls this lamnda funtion is not going to use it anymore, it passes as reference. However I think this is not how shared_ptr was designed to be used, and instead if the class wants to do that, it should move an unique_ptr like this:
viedoTrack->addDelegate(std::make_shared<FrameWriterInterfaceHelper>([this](std::unique_ptr<Frame> frame) {
//...
So, how would you design this class's lambda function?
Aucun commentaire:
Enregistrer un commentaire