Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::make_shared number of parameters in the constructor

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.

like image 943
Robinson Avatar asked Apr 04 '12 13:04

Robinson


People also ask

What does Make_shared return in C++?

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.

What happens if Make_shared fails?

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 .

What is boost :: Make_shared?

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.

Does Make_shared call copy constructor?

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?


1 Answers

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)));
like image 52
111111 Avatar answered Sep 20 '22 22:09

111111