简体   繁体   English

进一步传递可变参数

[英]Pass variadic parameters further

I have a logging method called trace :我有一个名为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:现在我想创建一个具有相同接口的包装函数,它将调用我的trace函数:

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.我认为我不允许以这种方式传递args 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)...);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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