Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8618655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:09:39+00:00 2026-06-12T06:09:39+00:00

Herb Sutter propose a simple implementation of make_unique() there: http://herbsutter.com/gotw/_102/ Here it is: template<typename

  • 0

Herb Sutter propose a simple implementation of make_unique() there: http://herbsutter.com/gotw/_102/

Here it is:

template<typename T, typename ...Args>
std::unique_ptr<T> make_unique( Args&& ...args )
{
    return std::unique_ptr<T>( new T( std::forward<Args>(args)... ) );
}

My problem is that variadic templates are not yet part of VS2012, so I can’t use this code as is.

Is there a maintainable way to write this in VS2012 that wouldn’t involve copy-pasting the same function with different args count?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T06:09:40+00:00Added an answer on June 12, 2026 at 6:09 am

    You could use Boost.Preprocessor to generate the different parameter counts, but I really don’t see the advantage of that. Simply do the grunt job once, stuff it in a header and be done. You’re saving yourself compile time and have your make_unique.

    Here‘s a copy-paste of my make_unique.h header that simulates variadic templates for up to 5 arguments.


    Since OP seems to not like copy-paste work, here’s the Boost.Preprocessor code to generate the above:

    First, make a main header that includes the template header multiple times (Boost.Preprocessor iteration code blatantly stolen from this answer):

    // make_unique.h
    #include <memory>
    #include <utility>
    #include <boost/preprocessor.hpp>
    
    #ifndef MAKE_UNIQUE_NUM_ARGS
    // allow this to be changed to a higher number if needed,
    // ten is a good default number
    #define MAKE_UNIQUE_NUM_ARGS 10
    #endif
    
    #if MAKE_UNIQUE_NUM_ARGS < 0
    // but don't be stupid with it
    #error Invalid MAKE_UNIQUE_NUM_ARGS value.
    #endif
    
    /* optional, see above for premade version
    // include premade functions, to avoid the costly iteration
    #include "detail/blah_premade.hpp
    
    // generate classes if needed
    #if MAKE_UNIQUE_NUM_ARGS > MAKE_UNIQUE_NUM_PREMADE
    */
    #define BOOST_PP_ITERATION_LIMITS (0, MAKE_UNIQUE_NUM_ARGS)
    #define BOOST_PP_FILENAME_1 "make_unique_template.h"
    #include BOOST_PP_ITERATE()
    //#endif
    

    And now make a template header that gets included again and again and expands differently depending on the value of MAKE_UNIQUE_NUM_ARGS:

    // make_unique_template.h
    // note: no include guard
    
    #define N BOOST_PP_ITERATION()    
    
    #define MAKE_UNIQUE_TEMPLATE_PARMS \
      BOOST_PP_ENUM_PARAMS(N, typename A)
    
    #define MAKE_UNIQUE_FUNCTION_PARM(J,I,D) \
      BOOST_PP_CAT(A,I)&& BOOST_PP_CAT(a,I)
    
    #define MAKE_UNIQUE_FUNCTION_PARMS \
      BOOST_PP_ENUM(N, MAKE_UNIQUE_FUNCTION_PARM, BOOST_PP_EMPTY)
    
    #define MAKE_UNIQUE_ARG(J,I,D) \
      std::forward<BOOST_PP_CAT(A,I)>(BOOST_PP_CAT(a,I))
    
    #define MAKE_UNIQUE_ARGS \
      BOOST_PP_ENUM(N, MAKE_UNIQUE_ARG, BOOST_PP_EMPTY)
    
    template<class T BOOST_PP_COMMA_IF(N) MAKE_UNIQUE_TEMPLATE_PARMS>
    std::unique_ptr<T> make_unique(MAKE_UNIQUE_FUNCTION_PARMS){
      return std::unique_ptr<T>(new T(MAKE_UNIQUE_ARGS));
    }
    
    // clean up
    #undef MAKE_UNIQUE_TEMPLATE_PARMS
    #undef MAKE_UNIQUE_FUNCTION_PARM
    #undef MAKE_UNIQUE_FUNCTION_PARMS
    #undef MAKE_UNIQUE_ARG
    #undef MAKE_UNIQUE_ARGS
    #undef N
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

According to Herb Sutter the code below wouldn't compile. See this site http://www.gotw.ca/gotw/066.htm from
I'm trying to implement a delegate class following Herb Sutter's Example . There is
From Herb Sutter's GotW #6 Return-by-value should normally be const for non-builtin return types.
http://www.drdobbs.com/cpp/practical-c-error-handling-in-hybrid-env/197003350?pgno=4 In this article Herb Sutter explains that throwing an exception requires a copy
I've just seen Herb Sutter's presentation Future C++ and there was mentioned that he
From what I've read from Herb Sutter and others you would think that volatile
I have been reading Exceptional C++ by Herb Sutter . On reaching Item 32
I am reading book called C++ coding standard By Herb Sutter, Andrei Alexandrescu and
Referring to article Gotw 54 by HerbSutter, he explains about The Right Way To
Herb Sutter has said that the most object oriented way to write methods in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.