Andreas Loanjoe
Andreas Loanjoe

Reputation: 2399

C++ Tuple types to function parameter types

Let's say I have a function that should take the arguments that should match the types of a tuple, I could write it like this:

using ArgsTuple = std::tuple<int, float>;

template<typename... Args, 
    class = typename std::enable_if<std::is_convertible<std::tuple<Args...>, ArgsTuple>::value>::type>
void function(Args... args)
{
}

But this is actually different then really having int and float as parameters, it takes all types of parameters and then constraints them to only allow matching types. How would I be able to unpack the tuple in such a way that the function will actually have the types in ArgsTuple as parameters, so that for example visual studio would be able to auto-complete the types required to call the function. The resulting function must be a free function.

Upvotes: 3

Views: 527

Answers (1)

max66
max66

Reputation: 66240

Not sure to understand what do you want, and not exactly a free function (but a static method in a template class), but I suppose that you're asking for something similar as the follows

template <typename>
struct proFunc;

template <template <typename ...> class C, typename ... Ts>
struct proFunc<C<Ts...>>
 {
   static void func (Ts ...)
    { }
 };

that you can use this way

using ArgsTuple = std::tuple<int, float>;

proFunc<ArgsTuple>::func(1, 2.f);

and you can also verify that

static_assert( std::is_same<decltype(&proFunc<ArgsTuple>::func),
                            void(*)(int, float)>::value, "!" );

To obtain something more similar to a free function, you can use function pointers

auto  funcPnt = &proFunc<ArgsTuple>::func;

funcPnt(3, 4.f);

Upvotes: 4

Related Questions