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

前端 未结 6 1349
情书的邮戳
情书的邮戳 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:30

    I know I am late to the party here, but I just came across this. I have been doing this with a one-liner macro (and is VS2012 compatible):

    #define MAKE_UNIQUE(T, ...) std::unique_ptr(new T(__VA_ARGS__))
    

    Or less portable, but you can pass in empty params

    #define MAKE_UNIQUE(T, ...) std::unique_ptr(new T(##__VA_ARGS__))
    

    Use like this:

    // MAKE_UNIQUE takes the type followed by the arguments list ...
    std::unique_ptr pPointer = MAKE_UNIQUE(MyClass, param_1, param_2, ..., param_n);
    

提交回复
热议问题