在编译时将 mpl::vector_c 复制到静态数组
Copy an mpl::vector_c to a static array at compile time
有了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;
}
我想使用 mpl::vector_c 中的值"包含"初始化 boost::array 内容。此初始化应在编译时执行。我已经在SO上看到了一些使用预处理器的解决方案,但我不知道如何将它们应用于可变参数模板情况。
请注意,在上面的示例代码中,mpl::vector_c 的元素与 Test 的模板参数相同。在实际代码中并非如此,相反values_type
具有长度 == 模板参数的数量,但实际值来自一系列 mpl 算法的应用。因此,不要假设参数相同。
希望问题清楚,谢谢!
一种方法是使用 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("%zun", s);
return 0;
}
我在这里使用std::array
,但您可以简单地将其更改为boost::array
它仍然有效。表达式
to_std_array<MPLVector>::make()
在编译时运行,因为make()
函数constexpr
。
相同的技术通常用于将std::tuple
扩展为std::array
(将 std::tuple 转换为 std::array C++11),转换为函数调用("解包"元组以调用匹配的函数指针)等。
相关文章:
- 在数组中使用C++中的哈希表复制数组中的元素
- 快速复制数组到阵列
- 如何在C 中复制数组
- 赋值运算符复制数组中除可用空间之外的所有内容
- 尝试复制数组时从 DLL 调用函数时程序崩溃
- 反向复制数组
- 复制数组 - 将数组加倍后崩溃
- 复制数组和气泡排序 C++
- 在结构中复制构造函数:是否复制数组
- 如何从类中复制数组并将新数组的大小加倍
- 在C++中复制数组的麻烦
- 逐行复制数组的内容
- tictactoe和复制数组
- 正在使用数组的名称strring复制数组
- c++复制数组到数组
- . net互操作是来回复制数组数据,还是固定数组?
- 从通过函数发送的结构体中复制数组
- 使用CUDA Thrust置换迭代器复制数组的特定元素
- C/ c++中有复制数组的函数吗?
- 在引用中复制数组