带有模板化运算符<<的带条件调试输出类

rub*_*nvb 3 c++ debugging templates

我正在尝试创建一个简单的类似qDebug的类,我可以用来在调试模式下输出调试消息,这取决于调用应用程序时传递的一些调试级别.我喜欢QDebug类的易用性(它可以用作std :: cerr,并且在发布模式下编译时会消失).到目前为止我有这个:

#ifdef DEBUG
    static int CAKE_DebugLevel;

    class Debug
    {
        Debug( int level ) : m_output( level <= CAKE_DebugLevel ) {}

        template<typename T>
        Debug& operator<<( T )
        {
            if( m_output )
            {
                std::cout << T;
                return *this;
            }
            else
                return *this;
        }
    private:
        bool m_output;
    };
#else // release mode compile
    #define Debug nullstream
#endif // DEBUG
Run Code Online (Sandbox Code Playgroud)

我认为nullstream对于发布模式定义来说,最好的东西是最好的:

class nullstream{};

template <typename T>
nullstream& operator<<(nullstream& ns, T)
{
    return ns;
}
Run Code Online (Sandbox Code Playgroud)

主要是我暂时:

#include "Debug.h"

#include <iostream>

int main()
{
    Debug(0) << "Running debug version of CAKE." << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

这与gcc 4.5.1给出以下错误:

在成员函数'Debug&CAKE_Debug :: operator <<(T)'中:在';'之前的预期primary-expression 令牌(点在线std::cout << T;)

在函数'int main(int,char**,char**)'中:在'<<'标记之前的预期unqualified-id

我确信这很简单,但我发现的所有一般模板信息都没有.谢谢你的帮助!

如果您需要更多信息,请询问.

Mar*_*ork 6

其他人指出了普通物体的错误.

template<typename T> 
Debug& operator<<(T const& value)
{
    if ( m_output )  
    {  
        std::cout << value;  
    }  
    return *this;  
}
Run Code Online (Sandbox Code Playgroud)

但是你还需要一种输出std :: endl和其他操纵器的方法.由于上面的模板将无法正确处理这些.为此,您需要一个操作符来处理操作流的函子(即所有的iomanipulator(如std :: endl)).

// Use a typedef to make the code readable.
// This is a function pointer that takes a stream as input and returns the stream.
// This covers functions like std::endl
typedef std::ostream& (*STRFUNC)(std::ostream&);

D& operator<<(STRFUNC func)  // Inside the class
{
    if ( m_output )  
    {  
        // Apply the function
        func(std::cout);
    }
    // But return the debug object
    return *this;
}
Run Code Online (Sandbox Code Playgroud)

处理普通和广泛流的实现:

#include <iostream>

#if defined(_DEBUG)
#define DEBUG_LOG_TEST_CONDITION        output
#define DEBUG_LOG_DEBUG_TEST_LEVEL(v)   (v) <= DebugCake::DebugLevel
#else
#define DEBUG_LOG_TEST_CONDITION        false
#define DEBUG_LOG_DEBUG_TEST_LEVEL(v)   false
#endif

template<typename C,typename T = std::char_traits<C> >
struct DInfo
{
    typedef std::basic_ostream<C,T>& (*StrFunc)(std::basic_ostream<C,T>&);
    static std::basic_ostream<C,T>& getDefault();
};

struct DebugCake
{
    static int DebugLevel;
};
template<typename C,typename T = std::char_traits<C> >
struct D
{

    D(int level)
        :stream(DInfo<C,T>::getDefault())
        ,output( DEBUG_LOG_DEBUG_TEST_LEVEL(level) )
    {}
    D(int level, std::basic_ostream<C,T>& s)
        :stream(s)
        ,output( DEBUG_LOG_DEBUG_TEST_LEVEL(level) )
    {}

    template<typename V>
    D& operator<<(V const& value)
    {
        // In release this is optimised away as it is always false
        if (DEBUG_LOG_TEST_CONDITION)
        {
            stream << value;
        }
        return *this;
    }

    D& operator<<(typename DInfo<C,T>::StrFunc func)
    {
        // In release this is optimised away as it is always false
        if (DEBUG_LOG_TEST_CONDITION)
        {
            func(stream);
        }
        return *this;
    }
    private:
       std::basic_ostream<C,T>&  stream;
       bool                      output;

};

template<>
std::ostream&  DInfo<char,std::char_traits<char>       >::getDefault()
{return std::cout; }

template<>
std::wostream& DInfo<wchar_t,std::char_traits<wchar_t> >::getDefault()
{return std::wcout; }

typedef D<char>    Debug;
typedef D<wchar_t> WDebug;
int DebugCake::DebugLevel = 4;


int main()
{
    Debug   debug(1);

    debug << "Plop" << std::endl;

    WDebug  debugWide(2);
    debugWide << L"WIDE" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)


Pup*_*ppy 6

你的nullstream类没有整数构造函数.这意味着当你#define Debug nullstream时,编译器无法识别Debug(0) - 这没有任何意义.Debug不是一个接受参数的宏,如果用nullstream替换,则nullstream没有带参数的构造函数.用这种方式#define是错的哦.你应该有这样的东西:

#ifdef _DEBUG
static int CAKE_Debuglevel;
#endif

class Debug
{
    Debug( int level ) 
#ifdef _DEBUG
    : m_output( level <= CAKE_DebugLevel ) 
#endif
        {}

    template<typename T>
    Debug& operator<<( T t)
    {
        #ifdef _DEBUG
        if( m_output )
        {
            std::cout << t;
            return *this;
        }
        else
        #endif
            return *this;
    }
private:
#ifdef _DEBUG
    bool m_output;
#endif
};
Run Code Online (Sandbox Code Playgroud)

现在,您的类在任何环境中都会看起来和行为相同,但只有在定义了_DEBUG时才输出.我还修复了你试图输出类型的bug.