为什么variadic类模板最多能具有一个参数包?

问题描述 投票:0回答:1

有片刻,我希望我可以写一个由一个参数的类模板 标点的变异模板参数包列表,例如

template<typename ...lhs, int Punct, typename ...rhs>
struct tuple_pair
{
    std::tuple<lhs...> _lhs;
    std::tuple<rhs...> _rhs;
};

或为此:

template<int ...lhs, typename Punct, int ...rhs>
struct seq_pair
{
    std::integer_sequence<int,lhs...> _lhs;
    std::integer_sequence<int,rhs...> _rhs;
};
这些很可能是我希望肮脏的骇客的时刻,但无论如何 当然,标准说我不能:§14.1.11:

如果主要类模板或别名模板的模板参数是一个 模板参数包,它应为最后一个模板参数。

我不明白
为什么是这样。在我看来,在任何实例化中, 例如

tuple_pair<char,short,0,int,long> tp; seq_pair<0,2,3,void,4,5,6> sp; 编译器也可以将

...lhs
参数与
...rhs

区分开 我可以

i我没有邀请对为什么标准是什么 - 强调的猜测 - 但是可以
任何人
授权
告诉我们为什么C ++模板机械没有或
不能以这种方式支持多个类模板参数包的分离?
我特别想确认或驳回怀疑 
逃脱我的基本逻辑障碍。
    

Variadic模板列表不能被操纵为头等对象。结果,使用包裹在某些模板对象中的参数包工作通常更方便。

这是我将如何将两个类型列表传递给模板类的方式: // create an empty default implementation template <typename LeftTuple, typename RightTuple> class tuplePair {}; // specialise to allow tupled lists of types to be passed in template <typename ...LHS, typename ...RHS> class tuplePair<tuple<LHS...>, tuple<RHS...> > { // ... }; //or more catholically: template <typename ...LHS, typename ...RHS, template<typename...> class tuple_template> class tuplePair<tuple_template<LHS...>, tuple_template<RHS...>> { // ... }; template<typename... X> class some_other_tuple {}; int main() { tuplePair<tuple<char,char,char>, tuple<char,char,char>> tango_tuple; tuplePair<some_other_tuple<int>, some_other_tuple<char>> other_tuple; return 0; }

c++ variadic-templates standards
1个回答
4
投票
void

)更清晰。作为一般规则的语义,可以提供列表或元组对象而不是简单地使用定界符的语义更强大(因为它们允许嵌套)易于操纵。

addendum:

我将有其他问题的答案的风险,特别是不是权威的,希望它可能会有所帮助。 我已经阅读了variadic模板提案的草稿,并在comp.std.c ++上扫描了所有196个线程,提到了“ variadic”一词,看来这种限制的唯一原因是简单性。特别是起草标准而不是实现的简单性。
我找不到有关您提出的概括的讨论(允许参数包在模板参数列表中的任何位置,而不会随后是同类的参数或参数包)。然而,讨论了其他概括,例如允许参数包的扩展出现在模板结束以外的其他位置
特性化

,看起来时间就像是在这些讨论中耗尽的。

您有说服力的用例吗?我真的不是要成为“向我展示用例,否则我会把你关闭”
欺负,我只是感兴趣的。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.