如何从c++中的包装中删除元素?
我正在尝试从C ++包中删除元素。很难用单词解释,所以我只会向您展示我想要的代码。
// lets say I have the following function
template<typename... Args>
void foo1(Args... arguments)
{
// does something with the arguments
}
// and another similar function which needs to call foo1 but with a modified pack
template<typename... Args>
void foo2(Args... arguments)
{
// foo2 figures out what arguments should be removed from the "arguments" pack
// and here comes the hard part, after I know the indices of what elements to remove, how do I remove them from the pack?
// then foo2 calls foo1 with the new pack (modified argument list)
foo1(new_arguments...);
}
我想要一个纯的C ++解决方案,而无需包含任何文件,因为它应该适用于内核模式,并且您不能在内核模式下包含任何标准的C ++库。
有什么想法如何做?
编辑: 这些索引是constexpr整数值,因此我可以在模板或类似的内容中使用它们。
I'm trying to remove an element from a C++ pack. It's a hard to explain with words so I will just show you what I want in code.
// lets say I have the following function
template<typename... Args>
void foo1(Args... arguments)
{
// does something with the arguments
}
// and another similar function which needs to call foo1 but with a modified pack
template<typename... Args>
void foo2(Args... arguments)
{
// foo2 figures out what arguments should be removed from the "arguments" pack
// and here comes the hard part, after I know the indices of what elements to remove, how do I remove them from the pack?
// then foo2 calls foo1 with the new pack (modified argument list)
foo1(new_arguments...);
}
I want a pure C++ solution without including any files because it should work for kernel mode and you can't include any standard C++ library in kernel mode.
Any ideas how to do it?
EDIT:
The indices are constexpr integer values so I can use them in templates or anything like that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(2)
这是C ++ 20解决方案,无需使用任何标准库标头。
它定义了类型特质
取
,该从包装的前部收集n
类型的列表,然后使用列表来定义lambda,该lambda分区foo2 并在每次递归中删除
n
th索引,直到在委派foo1
委派之前,没有留下滴索引。Here's a C++20 solution without using any standard library headers.
It defines a type trait
take
which collects a list ofN
types from the front of a pack, and then uses the list to define a lambda that partitions the arguments offoo2
and drops theN
th index at each recursion until no drop indices are left before delegating tofoo1
.我找到了一个解决方案,这并不是我想要的,因为它使用
std :: tuple
,但现在已经足够了。这是代码:然后您可以调用这样的函数:
在此示例中,0和第三元素将从包中删除(函数调用中的第一个元组),这意味着
20.0
和<代码> 29.0 将被排除,add2
将使用30.0
和40.0
来调用。编辑:
我忘了发布该代码的这一部分:
I found a solution, it's not exactly what I wanted because it uses
std::tuple
but it's good enough for now. Here is the code:Then you can call the function for example like this:
In this example the 0 and 3rd element will be removed from the pack (first tuple in the function call) which means
20.0
and29.0
will be excluded andadd2
will be called with30.0
and40.0
.EDIT:
I forgot to post this part of the code: