c++ - 限制 boost make_shared 采用的参数

标签 c++ boost shared-ptr

以下代码编译没有问题。在这种情况下,我向 make_shared 发送 9 个参数

#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
class Controller
{
  int a1,a2,a3,a4,a5,a6,a7,a8,a9;

  static void createInstance(int a1,
                             int a2,
                             int a3,
                             int a4,
                             int a5,
                             int a6,
                             int a7,
                             int a8,
                             int a9)
  {
    Controller::controller = boost::make_shared<Controller>(a1,a2,a3,a4,a5,a6,a7,a8,a9);
  }

  private:

  Controller(int a1,
             int a2,
             int a3,
             int a4,
             int a5,
             int a6,
             int a7,
             int a8,
             int a9) { }
  static boost::shared_ptr<Controller> controller;
};

int main()
{
  Controller::createInstance(1,2,3,4,5,6,7,8,9);
}

以下代码无法编译。在这种情况下,我向 make_shared 发送 10 个参数

#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
class Controller
{
  int a1,a2,a3,a4,a5,a6,a7,a8,a9,a10;

  static void createInstance(int a1,
                             int a2,
                             int a3,
                             int a4,
                             int a5,
                             int a6,
                             int a7,
                             int a8,
                             int a9,
                             int a10)
  {
    Controller::controller = boost::make_shared<Controller>(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10);
  }

  private:

  Controller(int a1,
             int a2,
             int a3,
             int a4,
             int a5,
             int a6,
             int a7,
             int a8,
             int a9,
             int a10) { }
  static boost::shared_ptr<Controller> controller;
};

int main()
{
  Controller::createInstance(1,2,3,4,5,6,7,8,9,10);
}

编译时出现如下错误

In static member function ‘static void Controller::createInstance(int, int, int, int, int, int, int, int, int, int)’:  
error: no matching function for call to  ‘make_shared(int,int,int,int,int,int,int,int,int,int)’

为什么 make_shared 有 9 个参数的这个任意限制?如何获取超过 9 个参数?

最佳答案

您的编译器和/或库似乎不支持可变参数模板。

如果您希望此语法有效,您可能需要升级到支持可变参数模板的编译器并使用 STL 函数 std::make_shared

关于c++ - 限制 boost make_shared 采用的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30709499/

相关文章:

c++ - 为后期初始化转发参数

c++ - OpenCV 和 RTMP

c++ - 如何在 C++ 中将数组中的所有元素初始化为相同的数字

C++ 指向函数的友元指针

c++ - boost::log-在库/插件中使用独立的严重级别

c++ - 这个 std::vector 和 std::shared_ptr 内存泄漏是一个错误吗?

c++ - 二叉搜索树 - 按两个数据项排序

c++ - 使用 boost mpl 插入器迭代器的意外结果

c++ - 在Windows窗体c++/cli程序中为已定义的标识符获取未定义的标识符错误

c++ - vector<shared_ptr<X>> copying-调用了X构造函数?