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.

Alex Z
  • 1,867
  • 1
  • 29
  • 27
Robinson
  • 9,666
  • 16
  • 71
  • 115

2 Answers2

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)));
111111
  • 15,686
  • 6
  • 47
  • 62
  • The good thing is that make_shared will also use optimized one-allocation routine when creating shared_ptr – Alex Z Apr 04 '12 at 14:00
  • 1
    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? – Mordachai Feb 11 '13 at 21:06
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.

Roee Gavirel
  • 18,955
  • 12
  • 67
  • 94