lundi 4 décembre 2017

Enumerating boolean template parameters in C++11

I'm trying to determine how to take in a boolean template parameter pack

template<bool...B> struct BoolPack{};

and construct from it an integer parameter pack where the values indicate the enumeration of the true elements in BoolPack. The enumeration could be contained like this:

template<size_t...I> struct IntegerSequence{};

The function should take in a BoolPack<...B> and return an IntegerSequence<...I>;

template<bool...B>
constexpr auto enumerate( const BoolPack<B...>& b ) {
  // Combination of cumulative sum and mask?
  return IntegerSequence<???>();
}

For example, if the input would be

BoolPack<true,false,true,false,true> b;

The function should return

IntegerSequence<1,0,2,0,3> e();

My best guess on how to implement this would be to compute a partial sum where the the kth template parameter is

K = K-1 + static_cast<size_t>(get<K>(make_tuple<B...>));

I'm not sure if this is the way to do it. Is there not a more direct approach that doesn't require making tuples for example? Applying the recursion should result in

IntegerSequence<1,1,2,2,3> s();

And then to multiply this componentwise with the elements of the original BoolPack. Is this the way to go, or can I do this without tuples?

Thanks!

Aucun commentaire:

Enregistrer un commentaire