使用预处理器宏取消定义函数 [英] Undefining functions using preprocessor macros

查看:143
本文介绍了使用预处理器宏取消定义函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用C ++编写的日志系统,上面写有这种功能:

I've a log system written in C++ with this type of functions to write on it:

    void processMessages();
    void DEBUG_MSG(const std::string& appender,const char* msg, ...);
    void INFO_MSG(const std::string& appender,const char* msg, ...);
    void WARNING_MSG(const std::string& appender, const char* msg, ...);
    void ERROR_MSG(const std::string& appender, const char* msg, ...);
    void FATAL_MSG(const std::string& appender, const char* msg, ...);

我想通过C ++中的宏禁用。
我已阅读此线程:使用MACROS禁用功能,但

I want to disable via macros in C++. I've read this thread: Disable functions using MACROS but

#ifdef GLOG_SILENCE
        #define processMessages     (void)sizeof
        #define DEBUG_MSG           (void)sizeof
        #define INFO_MSG            (void)sizeof
        #define WARNING_MSG         (void)sizeof
        #define ERROR_MSG           (void)sizeof
        #define FATAL_MSG           (void)sizeof
#else //GLOG_SILENCE
        void processMessages();
        void DEBUG_MSG(const std::string& appender,const char* msg, ...);
        void INFO_MSG(const std::string& appender,const char* msg, ...);
        void WARNING_MSG(const std::string& appender, const char* msg, ...);
        void ERROR_MSG(const std::string& appender, const char* msg, ...);
        void FATAL_MSG(const std::string& appender, const char* msg, ...);
#endif //GLOG_SILENCE

无法正常工作。我不断收到类似这样的错误:

doesn't work properly. I keep getting errors like:


../ src / test_core.cpp:2中包含的文件:

In file included from ../src/test_core.cpp:2:

../src/test_Log.h: In member function ‘virtual void LogTestFixtureTest_defining_SILENCE_macro_avoids_write_and_processing_activity_from_log_Test::TestBody()’:
../src/test_Log.h:63: error: expected unqualified-id before ‘(’ token
../src/test_Log.h:63: error: expected primary-expression before ‘void’
../src/test_Log.h:63: error: expected ‘;’ before ‘sizeof’
../src/test_Log.h:64: error: expected unqualified-id before ‘(’ token
../src/test_Log.h:64: error: expected primary-expression before ‘void’
../src/test_Log.h:64: error: expected ‘;’ before ‘sizeof’


我怀疑问题与Log是一个类有关,但是我不知道知道怎么做。
有帮助吗?

I suspect that the problem is related with the fact that Log is a class, but I don't know how to do it. Some help?

推荐答案

实际上,如果这些是成员函数,则无提示版本将扩展为废话:

Indeed, if these are member functions, then the "silent" versions will expand to nonsense:

log.(void)sizeof(stuff);

您可以定义不执行任何操作的成员函数以及可吞噬其参数的宏:

You could define a member function that does nothing, and macros that swallow their arguments:

void nothing() {}

#define processMessages(...) nothing()

然后使用静默版本将给出有效的代码,这些代码应编译为空:

then using the "silent" versions will give valid code that should compile away to nothing:

log.nothing();

这样做的缺点是(a)您依靠编译器内联空函数,而不生成函数调用; (b)在静默模式下编译时,不会检查参数的语法。

The disadvantages of this are (a) you're relying on the compiler to inline the empty function, and not generate a function call; (b) the arguments' syntax is not checked when compiling in silent mode.

这篇关于使用预处理器宏取消定义函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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