I want to convert an \"array\" of bool
to a integer sequence.
So I need to compute an std::array
at compile time.
Here is my code
Is it possible to do otherwise?
About correctness answered JVApen (+1).
A possible alternative is avoid std::array
at all and construct the index sequence in a recursive way using template specialization
The following is a full compilable example
#include
#include
template
struct bar;
// true case
template
struct bar, I, true, Bs...>
: public bar, I+1U, Bs...>
{ };
// false case
template
struct bar, I, false, Bs...>
: public bar, I+1U, Bs...>
{ };
// end case
template
struct bar
{ using type = T; };
template
struct foo : public bar, 0U, Bs...>
{ };
int main()
{
static_assert( std::is_same::type,
std::index_sequence<1U, 2U>>{}, "!" );
}
If you don't like the recursive solutions, I propose (just for fun) another solution based of std::tuple_cat
#include
#include
#include
template
struct baz
{ using type = std::tuple<>; };
template
struct baz
{ using type = std::tuple>; };
template
using baz_t = typename baz::type;
template
struct bar;
template
struct bar, Bs...>
{
template
constexpr static std::index_sequence
func (std::tuple...> const &);
using type = decltype(func(std::tuple_cat(baz_t{}...)));
};
template
struct foo : public bar, Bs...>
{ };
int main()
{
static_assert( std::is_same::type,
std::index_sequence<1U, 2U>>{}, "!" );
}