简体   繁体   中英

Pass variadic parameters further

I have a logging method called trace :

template <typename... Args>
void trace(const LogLevel ll, QString&& msg, Args... args)
{
     // operate on input data
}

Now I would like to create a wrapper function, with the same interface, that would call my trace function:

template <typename... Args>
void wrapper(const LogLevel ll, QString&& msg, Args... args)
{
     trace(ll, std::move(msg), args);
}

But this will not compile. I think I am not allowed to pass args this way. How can this functionality be accomplished?

You need to expand the pack like

template <typename... Args>
void wrapper(const LogLevel ll, QString&& msg, Args... args)
{
     trace(ll, std::move(msg), args...);
}

Also note that you can use perfect forwarding as well so you don't cause copies. That would look like

template <typename... Args>
void wrapper(const LogLevel ll, QString&& msg, Args&&... args)
{
     trace(ll, std::move(msg), std::forward<Args>(args)...);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM