条件调试输出类与模板化运算符<< [英] conditional debug output class with templated operator<<

查看:97
本文介绍了条件调试输出类与模板化运算符<<的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  #ifdef DEBUG 
static int CAKE_DebugLevel;

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

template< typename T>
Debug&运算符<<<(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

我认为 nullstream -kind的东西最适合发布模式define:

  class nullstream {}; 

template< typename T>
nullstream& operator<<(nullstream& ns,T)
{
return ns;
}

在主要我有片刻:

  #includeDebug.h

#include< iostream>

int main()
{
Debug(0)< 运行CAKE的调试版本。 << std :: endl;
}

这是使用gcc 4.5.1时出现以下错误:


在成员函数'Debug& CAKE_Debug :: operator<<(T)':期望的primary-expression before';'token(点在行 std :: cout<< T;



在函数'int main(int,char * ,char *)':期望未限定ID之前'<


我相信这很简单,但是我发现的所有一般模板信息都没有。非常感谢您的帮助!



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

解决方案>

您的nullstream类没有整数构造函数。这意味着当你#define Debug nullstream,编译器不能识别Debug(0) - 这没有意义。调试不是一个接受参数的宏,如果你替换为nullstream,nullstream没有接受参数的构造函数。 #define用这种方式是哦这么错了。你应该有这样的:

  #ifdef _DEBUG 
static int CAKE_Debuglevel;
#endif

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

template< typename T>
Debug&运算符<<(T t)
{
#ifdef _DEBUG
if(m_output)
{
std :: cout< t;
return * this;
}
else
#endif
return * this;
}
private:
#ifdef _DEBUG
bool m_output;
#endif
};

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


I'm trying to create a simple qDebug-like class I can use to output debug messages in debug mode, dependant on some debug level passed when calling the app. I liked the ease of use of the QDebug class (which could be used as a std::cerr and would disappear when compiling in release mode). I have this so far:

#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

I think a nullstream-kind of thing would be best for the release mode define:

class nullstream{};

template <typename T>
nullstream& operator<<(nullstream& ns, T)
{
    return ns;
}

In main I have for the moment:

#include "Debug.h"

#include <iostream>

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

Which is giving the following error with gcc 4.5.1:

In member function 'Debug& CAKE_Debug::operator<<(T)': expected primary-expression before ';' token (points at the line std::cout << T;)

In function 'int main(int, char*, char*)': expected unqualified-id before '<<' token

I'm sure it's something simple, but all the general template info I've found turns up nothing. Thanks for any help!

If you need more info, please ask.

解决方案

Your nullstream class has no integral constructor. This means that when you #define Debug nullstream, the compiler can't recognize Debug(0) - that makes no sense. Debug is not a macro that takes arguments, and if you substitute with nullstream, nullstream has no constructor that takes arguments. #define used this way is oh so wrong. You should have something like this:

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

Now your class really will look and act the same in any environment but only output if _DEBUG is defined. I also fixed the bug where you tried to output a type.

这篇关于条件调试输出类与模板化运算符&lt;&lt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆