是否可以对参数包进行typedef? [英] Is it possible to typedef a parameter pack?
问题描述
是否可以对参数包进行typedef?例如
Is it possible to typedef a parameter pack? For example
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;
};
我想使用上述技术来实现以下
I want to using the above technique to implement the following
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
};
推荐答案
您可以将它们打包在元组
,或在任意的空类模板中(我更喜欢将其称为 pack
):
You can pack them in a tuple
, or in a arbitrary empty class template (I prefer to call it pack
):
template<typename... Args>
struct pack { };
template<class T, class... Args>
struct A
{
using args = pack<Args...>;
};
如果给了 A
例如在函数模板中,并且您想推断出 Args ...
,您可以这样操作:
If you are then given A
e.g. in function template and you want to deduce Args...
, you can do it like this:
template<typename... Args, typename A>
void f(pack<Args...>, A a) { /* use Args... here */ }
template<typename A>
void f(A a) { f(typename A::args(), a); }
pack
在情况下很方便像那样。否则,您需要其他一些方法来传递 args
而不实际传递包含数据的 tuple
(例如,将其包装到
pack
being empty is convenient in situations like that. Otherwise you'd need some other means to pass args
without actually passing a tuple
that contains data (e.g. wrapping it into yet another empty struct).
或者在类模板专业化中:
Or, in a class template specialization:
template<typename T, typename = typename T::args>
struct B_impl;
template<typename T, typename... Args>
struct B_impl <T, pack<Args...> >
{
// use Args... here
};
template<typename T>
using B = B_impl<T>;
我想这是@dyp提到的演绎和部分专业化的选择。
I guess these are the options of deduction and partial specialization that @dyp mentioned.
编辑这是对已编辑问题的答复。好的,这显然是XY问题。如果只需要 IntegralSequence
,则可以在C ++ 14中使用 std :: make_integer_sequence
或检查我对另一个问题的回答。
EDIT This is in response to the edited question. Ok, this is clearly an XY problem. If IntegralSequence
is all you need, you can use std::make_integer_sequence
in C++14 or check my answer to another question just a few minutes ago for an efficient implementation.
这篇关于是否可以对参数包进行typedef?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!