limit on parameters taken by boost make_shared

我的未来我决定 提交于 2019-12-10 18:19:41

问题


The following piece of code compiles without an issue. In this scenario I'm sending 9 parameters to make_shared

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

The following piece of code does not compile. In this scenario I'm sending 10 parameters to make_shared

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

I get the following error when trying to compile

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

Why does make_shared have this arbitary limit of 9 parameters? How do I take more than 9 parameters?


回答1:


Your compiler and/or library doesn't seem to support variadic templates.

If you want this syntax to work, you may want to upgrade to a compiler that support variadic template and use the stl function std::make_shared



来源:https://stackoverflow.com/questions/30709499/limit-on-parameters-taken-by-boost-make-shared

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!