lundi 4 septembre 2017

Initialize a constexpr std::array with the size of an n-dimensional std::array

I wrote a function which takes an N-dimensional std::array and a parameter pack (coordinates) equal to the number of dimensions of the input std::array. I already can estimate the size of each dimension of the std::array with a meta function and I wrote a functions counting the parameters in the pack.

I want to 1) generate a new constexpr std::array with size equal to the number of dimensions of the input std::array. 2) The array shall be initialized with the size of each dimension of the input std::array. Does someone have a tip how to fill the std::array right with C++11 only.

E.g. this code

using array3d = std::array<std::array<std::array<int, 4>, 4>, 4>;
3d_helper<array3d>(array3d(), 0,0,0);

Should generate:

constexpr array3d array = { 4, 4, 4 };

Here is what I have so far:

//! Static estimation of std::array container size
// Declare a generic template (which is called initially)
template <size_t dim, class Array>
struct static_size;

// specialization for std::array and first dimension
// creates a struct with a static member "value = N"
template <class T, size_t N>
struct static_size<0, std::array<T, N>> : std::integral_constant<size_t, N> {};

// specialization for std::array and dimension > 0 -> recurse down in dim
template <size_t dim, class InnerArray, size_t N>
struct static_size<dim, std::array<InnerArray, N>> : static_size<dim - 1, InnerArray> {};

template <class FIRST, class... OTHER>
size_t num_args() {
    return 1 + num_args<OTHER...>();
}

template <class FIRST>
size_t num_args() {
    return 1;
}

template <class ARRAY, class... ARGS>
struct 3d_helper {
    static glm::vec3 at_t(const ARRAY &points, ARGS... args) {
        constexpr size_t nargs = num_args<ARGS...>();
        /*
        constexpr size_t n1 = static_size<0, ARRAY>::value - 1;
        constexpr size_t n2 = static_size<1, ARRAY>::value - 1;
        */
        // ...
        using array_t = std::array<size_t, nargs>;
        // fill it somehow
    }   
};

Aucun commentaire:

Enregistrer un commentaire