在编译时将mpl :: vector_c复制到静态数组

Giu*_*ano 5 c++ templates boost boost-mpl c++11

使用C++ 11我有类似的东西

#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;
}
Run Code Online (Sandbox Code Playgroud)

我想用mpl :: vector_c中的值'contains'初始化boost :: array内容.此初始化应在编译时执行.我已经在SO上看到了一些使用预处理器的解决方案,但我不知道如何将它们应用于可变参数模板的情况.

请注意,在上面的示例代码中,mpl :: vector_c的元素与Test的模板参数相同.在实际代码中并非如此,而是values_type具有length ==模板参数的数量,但实际值是由一系列mpl算法的应用产生的.因此,不要假设参数是相同的.

希望问题很清楚,谢谢!

ken*_*ytm 8

一种方法是使用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>{});
    }
};
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

我在std::array这里使用,但你可以简单地改变它boost::array,它仍然有效.表达方式

to_std_array<MPLVector>::make()
Run Code Online (Sandbox Code Playgroud)

在编译时运行,因为make()函数是constexpr.


相同的技术通常用于将a扩展std::tuplestd::array(将std :: tuple转换为std :: array C++ 11),函数调用("解包"一个元组以调用匹配的函数指针)等.