Peter Ruderman Peter Ruderman - 1 month ago 7
C++ Question

How can I convert each parameter in a variadic template function to another type and obtain its address?

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 template function 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.)

Answer

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({ (&parameters)... });
}

template <class... Params>
void Foo(Params... parameters)
{
    FooImpl(Wrapper<Params>(parameters)...);
}