Question

I have an array I want to initialise as a constexpr based on template paramaters (I think this will require c++14, as I envisage the answer requiring initialiser lists as constexpr).

Lets say I have a template

template<T t>

where

 T = int[1][2][3]

now, i can extract the array sizes recursively using type_traits std::extent

what I'd like to do ultimately is generate a constexpr member with the dimensions of T as elements of myarray

std::array<int,3> myarray = {1,2,3};

I've seen what looks like a nice way to initialise an array using a variadic template ref: How to construct std::array object with initializer list?

The question is, how to generate either an initialiser list or variadic template with the dimensions of T given T?1

Was it helpful?

Solution

The following is a bit complicated but it should work (with C++11):

#include <array>
#include <type_traits>

template<std::size_t...> struct seq {};

template<typename,typename> struct cat;
template<std::size_t... Is, std::size_t... Js>
struct cat<seq<Is...>,seq<Js...>>
{
    using type = seq<Is...,Js...>;
};

template<typename> struct extract_seq { using type = seq<>; };
template<typename T,std::size_t N>
struct extract_seq<T[N]>
{
    using type = typename cat< seq<N>, typename extract_seq<T>::type >::type;
};

template<typename T> struct extract_type { using type = T; };
template<typename T,std::size_t N>
struct extract_type<T[N]>
  : extract_type<T>
{};

template<typename,typename> struct to_array_helper;
template<typename T, std::size_t... Is>
struct to_array_helper<T,seq<Is...>>
{
    constexpr static const std::array<T,sizeof...(Is)> value {{ Is... }};
};

template<typename T, std::size_t... Is>
constexpr const std::array<T,sizeof...(Is)>
to_array_helper<T,seq<Is...>>::value;

template<typename T>
struct to_array
  : to_array_helper<typename extract_type<T>::type,
                    typename extract_seq<T>::type>
{};

int main()
{
    auto arr = to_array< int[1][2][3] >::value;
}

Live example

OTHER TIPS

I don't think you need any special future C++. This works fine in C++11:

#include <array>
#include <iostream>
#include <type_traits>

#include <prettyprint.hpp>

template <typename T>
struct Foo
{
    std::array<std::size_t, std::rank<T>::value> a;

    Foo() : Foo(X<std::rank<T>::value>(), Y<>(), Z<T>()) { }

private:
    template <unsigned int K>  struct X { };
    template <unsigned int ...I> struct Y { };
    template <typename> struct Z { };

    template <typename U, unsigned int K, unsigned int ...I>
    Foo(X<K>, Y<I...>, Z<U>)
    : Foo(X<K - 1>(),
          Y<I..., std::extent<U>::value>(),
          Z<typename std::remove_extent<U>::type>())
    { }

    template <typename U, unsigned int ...I>
    Foo(X<0>, Y<I...>, Z<U>) 
    : a { I... }
    { }
};

int main()
{
    Foo<char[4][9][1]> x;
    std::cout << x.a << std::endl;
}

Outputs:

[4, 9, 1]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top