【发布时间】:2016-07-13 14:42:31
【问题描述】:
我想使用 c++14 可变参数模板构建一个编译时查找表。 目前我在那里:
static const unsigned kCount = 5;
template<unsigned Index>
constexpr auto getRow(void)
{
return std::array<unsigned, 2> { Index, Index * Index };
}
template<unsigned... Indices>
constexpr auto generateTable(std::index_sequence<Indices...>)
{
return std::array<std::array<unsigned, 2>, sizeof...(Indices)>
{
// This is were I'm stuck. How to build a std::array using Indices as template parameter in getRow()?
};
}
constexpr auto generate(void)
{
return generateTable(std::make_index_sequence<kCount>{});
}
我希望表格位于std::array 中。每行包含一个 std::array 和 2 列。我被困在generateTable() 中,我需要以某种方式将我的索引作为模板参数传递给getRow()。
这是否可以使用std::integer_sequence 和模板参数包扩展来实现,还是我需要自己实现递归?
(getRow() 被简化了 - 值类型实际上来自模板类型。Index * Index 只是一个占位符。我需要知道如何使用参数包扩展调用getRow()。)
【问题讨论】:
-
Index是什么?行号? -
是的,Index 是行号。目前从 0 运行到 4。
标签: c++ c++11 c++14 variadic-templates stdarray