简体   繁体   中英

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

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