简体   繁体   English

调试打印解包可变参数模板函数参数

[英]Debug printing unpacking variadic template function arguments

I am trying to create a general debug print function.我正在尝试创建一个通用的调试打印功能。

enum class DebugLevel : uint8_t
{
    INFO     = 0,
    EVENT    = 1,
    WARNING  = 2,
    ERROR    = 3,
    CRITICAL = 4
};

DebugLevel generalDebugLevel = DebugLevel::INFO;

template <typename ...T>
void DPRINT (DebugLevel dbgLevel, T&& ...args)
{
    if (dbgLevel >= generalDebugLevel)
    {
       std::cerr << __FILE__ << ":" << __LINE__ << " " << args... << std::endl;
    }
}

As you can see, I need to unpack as I pass it to <<.如您所见,我需要在将其传递给 << 时解压缩。

Any clues?有什么线索吗?

template <typename ...T>
void DPRINT (DebugLevel dbgLevel, T&& ...args)
{
    if (dbgLevel >= generalDebugLevel)
    {
       std::cerr << __FILE__ << ":" << __LINE__ << " ";
       using expander = int[];
       (void)expander{0, (void(std::cerr << std::forward<T>(args) << " "),0)...};
       std::cerr << std::endl;
    }
}

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

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