Is there a way to write make_unique() in VS2012?

前端 未结 6 1350
情书的邮戳
情书的邮戳 2020-12-03 07:44

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

Here it is:

template

        
6条回答
  •  -上瘾入骨i
    2020-12-03 08:42

    Even though variadic templates are not part of VS2012, there are macros built into the header file that help simulate them.

    See this very nice answer which shows how to implement make_unique in just a few cryptic lines. I confirmed that it works well:

    #include  // brings in TEMPLATE macros.
    #define MAKE_UNIQUE(TEMPLATE_LIST, PADDING_LIST, LIST, COMMA, X1, X2, X3, X4)   \
      template  \
      inline std::unique_ptr make_unique(LIST(_TYPE_REFREF_ARG))  \
      {  \
          return std::unique_ptr(new T(LIST(_FORWARD_ARG)));  \
      }
    _VARIADIC_EXPAND_0X(MAKE_UNIQUE, , , , )
    #undef MAKE_UNIQUE
    

提交回复
热议问题