I am trying to implement a function that can take an arbitrary number of std::size_t
variables and use them accordingly.
I wish to be able to call the function (let's call it Foo
for the sake of argument) as follows:
Foo( some_other_type, 1, 2, 5, 7, 10 );
I tried the following:
template <typename T, std::size_t... Coords>
T Foo( T tSomething, Coords... coords );
But that doesn't work, and if I removed the last parameter so it was valid C++ syntax, I would have to call it as follows Foo<1, 2, 5, 7, 10>( some_other_type );
Which is not what I wanted.
I could use variadic arguments:
template <typename T>
T Foo( T tSomething, ... );
But then I would lose the type-safety that I am looking for. So my question is, is there a way in C++11 aside from say using std::initializer_list<std::size_t>
, which would then require me to call Foo
as follows:
Foo( some_other_type, {1, 2, 5, 7, 10} );
And would mean I couldn't unpack the arguments to construct another type.
EDIT: The reason I wish to have something similar to variadic templates and why std::initializer_list
or a recursive version of Foo
won't work, is say I have a class Bar
whose constructor requires a variable number of arguments depending on the specific instantiation, I wish to do something similar to the following:
template <typename T, std::size_t... Coords>
T Foo( T tSomething /*, Coords... coords*/ )
{
Bar<sizeof...(Coords)> bar( /*coords...*/ Coords... );
// Do stuff...
return bar.tProperty;
}
But I wish to be able to call Foo
as stated above rather than with a template list.