Search code examples
c++visual-studio-2010visual-c++c++11shared-ptr

How to work around 10 limit for make_shared in Visual Studio


While utilizing new features of C++10 on some old code, I ended up with the problem that I cannot call make_shared that takes 12 parameters. I remember Microsoft's STL talking how they use emulation for make_shared and that 10 is maximum. Obviously refactoring code just for this is out of the question, so basically my question is - Is there a way to get more than 10 params to make_shared in VS 2010.


Solution

  • make_shared<foobar>(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
    

    can be replaced with

    shared_ptr<foobar>(new foobar(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12));
    

    In C++11, std::make_shared is actually a performance optimization over creating an object using the second method, because it only performs one memory allocation instead of two, but once you get past 10 variables, you don't have much choice about which to use.