Reputation: 12485
This question is similar to others that have been asked here, but with a small twist. I have a regular function something like this:
void Bar(std::initializer_list<Base*> objects);
Now I want to create a function template that wraps each parameter in a type that derives from Base
and passes them to the function above. Something like:
template <class... Params> void Foo(Params... parameters)
{
Bar({&Wrapper<Params>(parameters)...});
}
(The class Wrapper
is specialized for various types.) This code actually compiles in MSVC but the compiler issues a warning because I'm taking the address of an rvalue (which is disallowed by the standard). Is there an easy, standards conformant way to achieve the same thing? (I think I can do this with tuples, integer_sequences, and a helper function, but I'd like to avoid that if possible.)
Upvotes: 1
Views: 76
Reputation: 27633
The issue is the Wrapper<T>
instances must exist at some address. The easy way to do this is to construct a std::tuple<Wrapper<Params>...>
instance. The annoying part is that you have to extract the contents back out using std::get<N>
. In C++14, std::index_sequence
exists to help you with this matter.
template <class... Params, std::size_t... Idx>
void FooImpl(std::tuple<Wrapper<Params>...>& tup, std::index_sequence<Idx...>)
{
Bar({ (&std::get<Idx>(tup))... });
}
template <class... Params>
void Foo(Params... parameters)
{
std::tuple<Wrapper<Params>...> tup(Wrapper<Params>(parameters)...);
FooImpl(tup, std::make_index_sequence<sizeof...(Params)>());
}
If your Bar
takes const Wrapper<T>*
s, another option is to use C++'s constant ref rules to your advantage.
template <class... Params>
void FooImpl(const Wrapper<Params>&... parameters)
{
Bar({ (¶meters)... });
}
template <class... Params>
void Foo(Params... parameters)
{
FooImpl(Wrapper<Params>(parameters)...);
}
Upvotes: 3