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?
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.hheader 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):
And now make a template header that gets included again and again and expands differently depending on the value of
MAKE_UNIQUE_NUM_ARGS: