Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to generate a variadic argument list containing N arguments of a given type?

There is a class template of the form:

template <typename ...T> Bag {};

with lots of useful functionality, but some implementation left to derived classes. And often this is instantiated with a repetition of one type N times. Instead of defining classes like:

class BoolBag3 : public Bag<bool, bool, bool> {};
class IntBag2 : public Bag<int, int> {};

I am interested in having a class like:

template<size_t N, typename T> class UniBag : public Bag< ... > {}

especially since some pure virtuals in Bag can be implemented in a generic way when all types are the same. What is the simplest / most elegant way to do this?

What I have done so far: Use the pattern in this answer to generate a TypeSequence:

template<class T> using InvokeType = typename T::type;  // InvokeType is an alias for T::type.
template<class S1, class S2> struct concat;

// Type sequence.

template<typename ... T> struct TypeSequence{ using type = TypeSequence; };

template<typename ... T, typename ... U> 
struct concat<TypeSequence<T ...>, TypeSequence<U ...>> : TypeSequence<T ..., U ...>{};
template<class S1, class S2> using Concat = InvokeType<concat<S1, S2>>;

template<typename T, size_t N> struct gen_type_seq;
template<typename T, size_t N> using GenTypeSeq = InvokeType<gen_type_seq<T, N>>;

template<typename T, size_t N> 
struct gen_type_seq : Concat<GenTypeSeq<T, N/2>, GenTypeSeq<T, N - N/2>>{};

template<typename T> struct gen_type_seq<T, 0> : TypeSequence<>{};
template<typename T> struct gen_type_seq<T, 1> : TypeSequence<T>{};

Then use an intermediate class to convert TypeSequence to the variadic argument list:

template <typename T> class UniBagHelper {};
template <typename ... T> class UniBagHelper<TypeSequence<T ...>> : public Bag<T ...> {};

template <typename T, size_t N> class UniBag : public UniBagHelper<GenTypeSeq<T, N>> {};

UniBag<bool, 4> logicBag;
like image 541
Aelian Avatar asked Dec 21 '25 18:12

Aelian


2 Answers

C++14 introduces std::index_sequence, so you might directly have:

template <typename T, std::size_t> using always_t = T;

template <typename T> struct BagTNHelper;

template <typename T, std::size_t ... Is>
struct BagTNHelper<T, std::index_sequence<Is ...>>
{
    using type = Bag<always_t<T, Is>...>;
};

template <typename T, size_t N>
using BagTN = typename BagTNHelper<T, std::make_index_sequence<N>>::type;

And then

using BoolBag3 = BagTN<bool, 3>; // Bag<bool, bool, bool>
using IntBag2 = BagTn<int, 2>; // Bag<int, int>
like image 163
Jarod42 Avatar answered Dec 23 '25 07:12

Jarod42


Something along these lines, perhaps:

template <typename T, size_t dummy>
using EatIndex = T;

template <typename T, std::size_t... I>
Bag<EatIndex<T, I>...> MakeBagN(std::index_sequence<I...>);

template<size_t N, typename T>
using BagN = decltype(MakeBagN<T>(std::make_index_sequence<N>()));

template<size_t N, typename T>
class UniBag : public BagN<N, T> {};

Demo

like image 30
Igor Tandetnik Avatar answered Dec 23 '25 07:12

Igor Tandetnik



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!