Noah
Noah

Reputation: 143

How to remove elements from variadic template argument?

I'm trying to remove the first element of a variadic template argument. The code goes like this:

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)...);
}

Upvotes: 0

Views: 918

Answers (2)

Noah
Noah

Reputation: 143

I just got a little help, and found the solution:

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"

Upvotes: 0

Sam Varshavchik
Sam Varshavchik

Reputation: 118435

How about trying the direct approach.

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)...);
}

(also fixed to use forwarding references, and added the obvious return )

Upvotes: 4

Related Questions