Copy mpl :: vector_c to a static array at compile time

With C ++ 11, I have something like

#include <boost/mpl/vector_c.hpp>
#include <boost/mpl/size.hpp>

#include <boost/array.hpp>

#include <iostream>

namespace mpl = boost::mpl;

template<std::size_t ... Args>
struct Test
{
            typedef mpl::vector_c<std::size_t, Args ...> values_type;

            static const boost::array<std::size_t, sizeof...(Args)> values;
};


int main (int argc, char** argv)
{
            Test<3,2,5,6,7> test;
            return 0;
}

I would like to initialize the contents of boost :: array with the values ​​contained in mpl :: vector_c. This initialization must be performed at compile time. I have seen on SO some solutions using the preprocessor, but I have no idea how to apply them to the case of the variation pattern.

Note that in the above code example, the elements mpl :: vector_c match the parameters of the test pattern. In the actual code, this is not the case , instead it values_typehas a length == of the number of template arguments, but the actual values ​​are the result of applying a sequence of mpl algorithms. Therefore, do not assume that the argument is the same.

, , !

+5
1

- at_c vector_c , .

#include <cstdio>
#include <array>
#include <boost/mpl/vector_c.hpp>
#include <boost/mpl/at.hpp>
#include <boost/mpl/size.hpp>
#include <utils/vtmp.hpp>
// ^ https://github.com/kennytm/utils/blob/master/vtmp.hpp

template <typename MPLVectorType>
class to_std_array
{
    typedef typename MPLVectorType::value_type element_type;
    static constexpr size_t length = boost::mpl::size<MPLVectorType>::value;
    typedef std::array<element_type, length> array_type;

    template <size_t... indices>
    static constexpr array_type
            make(const utils::vtmp::integers<indices...>&) noexcept
    {
        return array_type{{
            boost::mpl::at_c<MPLVectorType, indices>::type::value...
        }};
    }

public:
    static constexpr array_type make() noexcept
    {
        return make(utils::vtmp::iota<length>{});
    }
};

int main()
{
    typedef boost::mpl::vector_c<size_t, 3, 2, 5, 6, 7> values;

    for (size_t s : to_std_array<values>::make())
        printf("%zu\n", s);
    return 0;
}

std::array , boost::array, .

to_std_array<MPLVector>::make()

, make() constexpr.


a std::tuple std::array ( std:: tuple std:: array ++ 11), ( "" ) ..

+7

All Articles