Bind meta-sequence VT unboxing

template <int I> struct int_ {};

template < typename ... Pack >
struct thingy
{
    void call()
    {
       f(???);
    }
 };

When creating an instance, it should be:

struct thingy<int,char,double>
{
    void call()
    {
        f(int, int_<1>(), char, int_<2>(), double, int_<3>());
    }
}

What do you think can be done? How?

The only thing I can think of is having overloads for thingy with N different parameters, for example:

template < typename T0 > struct thingy<T0> { ... };
template < typename T0, typename T1 > struct thingy<T0,T1> { ... };

etc...

With the implementation of the call in each.

+5
source share
2 answers

It can be done

Yes of course.

How?

In a few steps.

  • You need to be able to create integers
  • You should be able to alternate between two sequences.

Let's start with a range of integers.

template <size_t...>
struct IntegralPack {};

template <size_t A, size_t... N>
IntegralPack<N..., A> push_back(IntegralPack<N...>);

template <size_t A, size_t... N>
IntegralPack<A, N...> push_front(IntegralPack<N...>);

template <size_t L, size_t H>
struct IntegralRangeImpl {
    typedef typename IntegralRangeImpl<L+1, H>::type Base;
    typedef decltype(push_front<L>((Base()))) type;
};

template <size_t X>
struct IntegralRangeImpl<X, X> {
    typedef IntegralPack<> type;
};

template <size_t L, size_t H>
struct IntegralRange {
     static_assert(L <= H, "Incorrect range");
     typedef typename IntegralRangeImpl<L, H>::type type;
};

The conversion step is quite simple (fortunately):

template <typename...>
struct TypePack {};

template <size_t... N>
TypePack<int_<N>...> to_int(IntegralPack<N...>);

So, the next difficulty is merging.

template <typename... As, typename... Bs>
TypePack<As..., Bs...> cat(TypePack<As...>, TypePack<Bs...>);

template <typename, typename> struct Interleaver;

template <>
struct Interleaver<TypePack<>, TypePack<>> {
  typedef TypePack<> type;
};

template <typename A0, typename B0, typename... As, typename... Bs>
struct Interleaver<TypePack<A0, As...>, TypePack<B0, Bs...>> {
  typedef typename Interleaver<TypePack<As...>, TypePack<Bs...>>::type Base;
  typedef decltype(cat(TypePack<A0, B0>{}, Base{})) type;
};

Introducing this as a whole:

template <typename... Pack>
struct thingy {
    typedef typename IntegralRange<1, sizeof...(Pack) + 1>::type Indexes;
    typedef decltype(to_int(Indexes{})) Ints;
    typedef typename Interleaver<TypePack<Pack...>, Ints>::type Types;

    void call() { this->callImpl(Types{}); }

    template <typename... Ts>
    void callImpl(TypePack<Ts...>) {
        f(Ts{}...);
    }
};

Tadaam!

+4
source

, , , . , - , , , , . , .

... "thingy < > " invoker. .

, thingy, , int ... ints , 1, . , , - ( ):

f(get(my_thingy, idx<1>), get(my_thingy, idx<2>), ...)

, idx < 2 > ... idx:

template < typename Fun, typename H, typename ... T, typename ... Args >
auto call(Fun f, thingy<H,T...> const& t, Args&& ... args) 
  -> decltype(call(f,static_cast<thingy<T...>const&>(t), get(t,idx<1>), std::forward<Args>(args)...)
{
    return call(f, static_cast<thingy<T...>const&>(t), get(t, idx<1>), args...);
}

template < typename Fun, typename ... Args >
auto call(Fun f, thingy<> const&, Args&& ... args)
   -> decltype(f(std::forward<Args>(args)...))
{
    return f(std::forward<Args>(args)...);
}

, get - const &... kinda pissing me off. , .

idx 1, , .

0

All Articles