简体   繁体   English

std::make_shared 构造函数中的参数个数

[英]std::make_shared number of parameters in the constructor

In the absence of variadic templates (still,) in Visual Studio 2010/2011.在 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. ,因为它有 13 个参数,我认为 make_shared 被限制在 arg0 到 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?除了使用new而不是 make_shared 之外,这里还有其他可能性吗?

Thanks.谢谢。

You can use construct a class which will then be moved into the heap allocated value.您可以使用构造一个 class ,然后将其移入堆分配值中。

MyMaterials.push_back(std::make_shared<Material>(
    Material(MyFacade, name, ambient, diffuse, specular, 
             emissive, opacity, shininess, shininessStrength, 
             reflectivity, bumpScaling, maps, mapFlags)));

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.用正确的值填充它,并将其作为他唯一的参数调用构造函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM