std::make_shared number of parameters in the constructor
Asked Answered
C

2

11

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.

Cudgel answered 4/4, 2012 at 13:41 Comment(0)
C
25

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)));
Clothesline answered 4/4, 2012 at 13:50 Comment(2)
The good thing is that make_shared will also use optimized one-allocation routine when creating shared_ptrSaragossa
Sorry - I don't see how this avoids copying from local stack (where Material() is being constructed) to the heap (where make_shared will actually place it). In this case, how can the copy possibly be avoided?Chuckle
F
1

you can create a "input struct" with all the relevant members.
fill it with the correct values and call the constructor with that as his only param.

Fled answered 4/4, 2012 at 13:56 Comment(1)
Also a good idea, but I want to avoid extraneous "random" structs for things like this :-).Cudgel

© 2022 - 2024 — McMap. All rights reserved.