Std :: bind a call to std :: make_shared

I am trying to create a functor that returns shared_ptr by calling std :: bind on std :: make_shared, but is the syntax superior to me or maybe this is not even possible? Something like the following, assuming the MyBar constructor accepts a reference constant for MyFoo:

std::function<std::shared_ptr<MyBar>(const MyFoo &)> functor = std::bind(&std::make_shared<MyBar>, std::placeholders::_1);
+3
source share
1 answer

You are almost there; you just need to specify additional arguments make_sharedto indicate the type of parameter to accept. They are usually output, but if you do not specify them in the binding expression, then it tries to build the object by default MyBar.

std::function<std::shared_ptr<MyBar>(const MyFoo &)> functor = 
    std::bind(&std::make_shared<MyBar,MyFoo const&>, std::placeholders::_1);
+7
source

All Articles