std::make_shared number of parameters in the constructor

Robinson picture Robinson · Apr 4, 2012 · Viewed 10.3k times · Source

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.

Answer

111111 picture 111111 · Apr 4, 2012

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)));