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

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

问题描述

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

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

我认为一个 nullstream 的东西最适合发布模式定义:

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

主要有:

#include "Debug.h"

#include <iostream>

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

哪些是gcc 4.5.1发生以下错误:

Which is giving the following error with gcc 4.5.1:


在成员函数Debug&标记( std :: cout<< T; )中的预期主表达式

在函数'int main(int,char **,char **)'中:在'<'标记

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.

推荐答案

您的nullstream类没有整数构造函数。这意味着当您#define Debug nullstream时,编译器无法识别Debug(0) - 这没有意义。调试不是一个接受参数的宏,并且如果用nullstream替代,则nullstream没有接受参数的构造函数。 #define这样用哦是这样错了。你应该有这样的东西:

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

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

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天全站免登陆