How does QDebug() << stuff; add a newline automatically?

前端 未结 4 1669
挽巷
挽巷 2020-12-30 05:11

I\'m trying to implement my own qDebug() style debug-output stream, this is basically what I have so far:

struct debug
{
#if defined(DEBUG)
             


        
4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-30 05:43

    When you write that this is the typical usage:

    debug() << "stuff" << "more stuff" << std::endl;
    

    are you definitely planning to construct a debug object each time you use it? If so, you should be able to get the behavior you want by having the debug destructor add the newline:

    ~debug()
    {
        *this << std::endl;
    
        ... the rest of your destructor ...
    }
    

    That does mean you cannot do something like this:

    // this won't output "line1" and "line2" on separate lines
    debug d;
    d << "line1";
    d << "line2";
    

提交回复
热议问题