In the absence of variadic templates (still!) in Visual Studio 2010/2011, a constructor that takes a lot of parameters can be problematic. For example the following won't compile:
MyMaterials.push_back(std::make_shared<Material>(MyFacade,
name,
ambient,
diffuse,
specular,
emissive,
opacity,
shininess,
shininessStrength,
reflectivity,
bumpScaling,
maps,
mapFlags));
, because it has 13 parameters and I think make_shared is limited from arg0 to arg9. The obvious work-around is two part construction, but I was hoping to avoid this. Is there any other possibility here, apart from use of new instead of make_shared?
Thanks.
It constructs an object of type T passing args to its constructor, and returns an object of type shared_ptr that owns and stores a pointer to it.
So, if you throw exception from your class' constructor, then std::make_shared will throw it too. Besides exceptions thrown from constructor, std::make_shared could throw std::bad_alloc exception on its own. Therefore, you don't need to check if the result of std::make_shared is nullptr .
The header file <boost/make_shared. hpp> provides a family of overloaded function templates, make_shared and allocate_shared , to address this need. make_shared uses the global operator new to allocate memory, whereas allocate_shared uses an user-supplied allocator, allowing finer control.
Its seesm straight-forward -- make_shared needs to copy/move the unnamed MyClass() temp you created explicitly into the shared object space it allocates. It uses the move constructor if it can, or the copy constructor if there is no move constructor. What do you expect to happen?
You can use construct a class which will then be moved into the heap allocated value.
MyMaterials.push_back(std::make_shared<Material>(
Material(MyFacade, name, ambient, diffuse, specular,
emissive, opacity, shininess, shininessStrength,
reflectivity, bumpScaling, maps, mapFlags)));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With