Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert tuple to function parameters

template<typename... Args>
class SomeClass
{
    using tuple_type = std::tuple<Args...>; // (ie: <bool,int,bool>)   
    tuple_type mytuple;
};

template<typename T, typename C, typename... I> // T is SomeClass
class SomeOtherClass
{       

      void fn(void(C::*f)(bool,int,bool)); // I want this
      // based on the T::tuple_type but I'm not sure how. 

};

I could simply use tuple_element 3 times if i knew the tuple has 3 elements only, but I don't know that.

like image 676
Gam Avatar asked Dec 04 '25 07:12

Gam


1 Answers

Write a generic type trait:

template <class C, class F>
struct mem_ptr;

template <class C, class F>
using mem_ptr_t = typename mem_ptr<C, F>::type;

And specialize it for tuple:

template <class C, class... Args>
struct mem_ptr<C, std::tuple<Args...>> {
    using type = void (C::*)(Args...);
};

And then use it:

void fun(mem_ptr_t<C, typename T::tuple_type> f);

This assumes you want void as the return type.


Could generalize this to splitting up the mem_ptr part from the tuple to func part:

template <class C, class F>
struct mem_ptr {
    using type = F C::*;
};

template <class C, class F>
using mem_ptr_t = typename mem_ptr<C, F>::type;

template <class R, class T>
struct tuple_to_func;

template <class R, class... Args>
struct tuple_to_func<R, std::tuple<Args...>> {
    using type = R(Args...);
};

template <class R, class T>
using tuple_to_func_t = typename tuple_to_func<R, T>::type;

In which case you'd want:

void fun(mem_ptr_t<C,
    tuple_to_func_t<void, typename T::tuple_type>
> f);
like image 110
Barry Avatar answered Dec 06 '25 23:12

Barry



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!