比较可变参数模板
如果我有两个可变参数模板参数 A
和 B
,如何在编译时确保 A
的所有成员的类型> 也是 B
子集的类型(顺序相同)?
人为的例子:
template<typename...A>
struct Foo {
template<typename...B>
static void bar()
{
}
}
...
Foo<Apple, Orange>:: template bar<Apple, Orange, Grape>(); // this compiles
Foo<Apple, Orange>:: template bar<Orange, Grape>(); // this doesn't
If I have two variadic template arguments, A
and B
, how can I ensure at compile-time that the types of all of the members of A
are also the types of a subset of B
(in the same order)?
Contrived example:
template<typename...A>
struct Foo {
template<typename...B>
static void bar()
{
}
}
...
Foo<Apple, Orange>:: template bar<Apple, Orange, Grape>(); // this compiles
Foo<Apple, Orange>:: template bar<Orange, Grape>(); // this doesn't
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于一般子集,我不知道,但如果您可以保证
B
的形式为A..., More...
,那么这可能会这样做:(抱歉,
var_equal
是一个糟糕的名称。它应该被称为更合适的名称,例如initial_equal
。)更新: 这是一般解决方案, Luc Danton 详细制定了(请参阅此处查看其精美样式的代码):
测试用例:
For a general subset I don't know, but if you can guarantee that
B
is of the formA..., More...
, then this may do:(Sorry,
var_equal
is a terrible name. It should be called something more appropriate, likeinitial_equal
.)Update: Here is the general solution, worked out in detail by Luc Danton (see here for his beautifully styled code):
Test case: