(Templates) Variable args

Hey all. Long time no see. Sadly I don't have the time to keep up with these forums like I used to. =( But I had a C++ question and figured the people here could help!

I want to do something like this:

1
2
3
4
5
template <typename T, ??? >  // <- not sure what should go in the '???'
T* func( ??? )
{
    return new T( ??? );
}



The idea being, 'func' takes any number of parameters, of any type. And I want all those extra parameters to be forwarded to the constructor of my T object. Similar to what make_unique does.

The end result being:
1
2
3
4
Foo* p = func<Foo>(1, "whatever", someobj);

// is the same as
Foo* p = new Foo(1, "whatever", someobj);


I never fully wrapped my head around variadic templates, and this is kind of a huge blind spot for me. Does anyone know how to accomplish this?

Thanks.
Last edited on
1
2
template < typename T, typename... ARGS > make_raw_do_not_use( ARGS&&... args )
{ return new T( std::forward<ARGS>(args)... ) ; }


Strongly favour std::make_unique http://en.cppreference.com/w/cpp/memory/unique_ptr/make_unique
over using raw owning pointers.
Perfect. Thanks JLBorges.

Yes I know to use make_unique. I was just giving a simplistic example of what I'm trying to do. My use case here is a bit different. ;)
Topic archived. No new replies allowed.