I have a function:
template <typename A>
Foo createFoo() {
std::initializer_list<std::size_t> list = { Object<A>::getIndex() } );
return Foo(list);
}
It is invoked like:
Foo foo = createFoo<Bar>();
It works correct, returning me an object of class Foo.
Is it possible to write a specialized function in case there're more than one parameter argument, something like this?
template <typename A1, typename A2, typename...Ax>
Foo createFoo() {
std::initializer_list<std::size_t> list = { Object<A1>::getIndex(), Object<A2>::getIndex()... } );
return Foo(list);
}
So that I invoke it this way:
Foo foo = createFoo<Bar, Fred, Wilma>();
It gives me an error:
error: expansion pattern ‘Object::getIndex()’ contains no argument packs
and points to the "..." part of the code.
I don't want to have a single function declared as
template <typename ...A>
Foo createFoo() {
std::initializer_list<std::size_t> list = { Object<A>::getIndex() } );
return Foo(list);
}
because, let's suppose the Foo class has optimized logics in case there's only one std::size_t item in the initializer_list and I want to utilize that in my API.
I use GCC version 4.8.4 with -std=c++11 option
Thank you.
std::initializer_list<std::size_t> list = { Object<A1>::getIndex(), Object<A2>::getIndex()... } );
when you put ... after a function call, it looks for parameter packs inside of that to expand, but A2 isn't a parameter pack, Ax is.
Why even have the A1 and A2?
template <typename...Ax>
Foo createFoo() {
Then you can just say Object<Ax>::getIndex()...
Or if you must enforce a certain number of parameters, then:
std::initializer_list<std::size_t> list = { Object<A1>::getIndex(),Object<A2>::getIndex(), Object<Ax>::getIndex()... } );
The ... expansion accepts an empty parameter pack, so this will work for 2 or more types.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With