【发布时间】:2014-07-03 18:05:23
【问题描述】:
假设我有一个静态存储持续时间的 constexpr 数组(已知界限):
constexpr T input[] = /* ... */;
我有一个需要打包的输出类模板:
template<T...> struct output_template;
我想实例化output_template 喜欢:
using output = output_template<input[0], input[1], ..., input[n-1]>;
一种方法是:
template<size_t n, const T (&a)[n]>
struct make_output_template
{
template<size_t... i> static constexpr
output_template<a[i]...> f(std::index_sequence<i...>)
{ return {}; };
using type = decltype(f(std::make_index_sequence<n>()));
};
using output = make_output_template<std::extent_v<decltype(input)>, input>::type;
我缺少更清洁或更简单的解决方案吗?
【问题讨论】:
-
您在寻找什么?更简单的接口或实现?
-
the indices trick 在这里有用吗?你没有模板索引参数可以在这里使用,就像你使用元组一样,但是......
-
using output = decltype(deduce(input))::unpack<input>;?不过,清洁度并不高。 -
a的@dyp 链接似乎是一个障碍(即使在适当的地方有更多constexpr):coliru.stacked-crooked.com/a/2c196c6f2a82cbfb -
这似乎是proposal n3601 的另一个潜在应用,据我所知,这将允许
using output = foo<input>;。
标签: c++ templates variadic-templates constexpr c++14