如何从可变模板参数中删除元素?
How to remove elements from variadic template argument?
我正在尝试删除可变参数模板参数的第一个元素。
代码如下:
template<typename ...T>
auto UniversalHook(T... args)
{
//I want to remove the first element of `args` here, how can I do that?
CallToOtherFunction(std::forward<T>(args)...);
}
尝试直接方法怎么样。
template<typename IgnoreMe, typename ...T>
auto UniversalHook(IgnoreMe && iamignored, T && ...args)
{
//I want to remove the first element of `args` here, how can I do that?
return CallToOtherFunction(std::forward<T>(args)...);
}
(也固定使用转发引用,并添加了明显的 return
)
我刚刚得到一点帮助,找到了解决方案:
int main()
{
Function(3,5,7);
return 0;
}
template<typename ...T>
auto CallToAnotherFunction(T&&... args)
{
(cout << ... << args);
}
template<typename ...T>
auto Function(T&&... args) {
/*Return is not needed here*/return [](auto&& /*first*/, auto&&... args_){
return CallToAnotherFunction(std::forward<decltype(args_)>(args_)...);
}(std::forward<T>(args)...);
}
//Output is "57"
我正在尝试删除可变参数模板参数的第一个元素。 代码如下:
template<typename ...T>
auto UniversalHook(T... args)
{
//I want to remove the first element of `args` here, how can I do that?
CallToOtherFunction(std::forward<T>(args)...);
}
尝试直接方法怎么样。
template<typename IgnoreMe, typename ...T>
auto UniversalHook(IgnoreMe && iamignored, T && ...args)
{
//I want to remove the first element of `args` here, how can I do that?
return CallToOtherFunction(std::forward<T>(args)...);
}
(也固定使用转发引用,并添加了明显的 return
)
我刚刚得到一点帮助,找到了解决方案:
int main()
{
Function(3,5,7);
return 0;
}
template<typename ...T>
auto CallToAnotherFunction(T&&... args)
{
(cout << ... << args);
}
template<typename ...T>
auto Function(T&&... args) {
/*Return is not needed here*/return [](auto&& /*first*/, auto&&... args_){
return CallToAnotherFunction(std::forward<decltype(args_)>(args_)...);
}(std::forward<T>(args)...);
}
//Output is "57"