【发布时间】:2014-05-22 23:42:16
【问题描述】:
是否可以 typedef 参数包?例如
template<class T, class... Args>
struct A
{
typedef T Type; // We typedef it, then its derived class can use it.
// How about for parameter packs?
// Option 1:
typedef Args Arguments;
// Option 2:
using Arguments = Args;
// Option 3: I can put in a tuple, but how can I untuple it to a pack
typedef tuple<Args...> Tuple;
};
我想使用上述技术来实现以下
template<int... VALUES>
struct IntegralSequence
{
enum { SIZE = sizeof...(VALUES) };
template <unsigned I>
struct At
{
enum { VALUE = typename tuple_element<I,
tuple<integral_constant<int, VALUES>...>>::type::value
};
};
};
template<unsigned N>
struct AscendingSequence
{
typedef IntegralSequence<AscendingSequence<N-1>::VALUES..., N> Type;
using VALUES = Type::VALUES; // if it works
};
template<>
struct AscendingSequence<1>
{
typedef IntegralSequence<0> Type;
using VALUES = Type::VALUES; // if it works
};
【问题讨论】:
-
“我可以放入一个元组,但我怎样才能将它解开成一个包” 使用偏特化、演绎或索引技巧。
-
你可以使用
Args... args; -
如果你可以粗略地描述用例,即你想在哪里解压它们,那么构建一个工作示例可能会更容易。 (@dyp 给出了三种可能性,但它们都适用于一组有限的用例)
标签: c++ templates c++11 variadic-templates