简体   繁体   English

在 C++ 中解释 #define PRINTF if(false) printf

[英]Interpreting #define PRINTF if(false) printf in C++

I am analyzing the following macro in C++:我正在分析 C++ 中的以下宏:

#define PRINTF if(false) printf

Does this mean, instead of typing printf(" .... ");这是否意味着,而不是键入printf(" .... "); one can just use PRINTF("...") ?可以只使用PRINTF("...")吗? Or is PRINTF activated only if a boolean in the code is true?还是仅当代码中的布尔值为真时才激活PRINTF

This seems to be made to toggle output on and off.这似乎是为了打开和关闭输出。 As it currently stands,按照目前的情况,

PRINTF("What ever");

will never print anything as it expands to永远不会打印任何东西,因为它扩展到

if(false) printf("What ever");

You can then toggle output on by changing然后,您可以通过更改来切换输出

#define PRINTF if(false) printf

to

#define PRINTF if(true) printf
#define PRINTF if(false) printf 

is a macro for PRINTF , which will replace the latter withPRINTF的宏,它将用

if(false) printf

in your code.在你的代码中。 So whenever you write所以每当你写

PRINTF("something")

it is translated to它被翻译成

if(false) printf("something")

ie, it is not executed.即,它没有被执行。 It is probably useful for debugging, when you may want your PRINTF "cancelled".当您可能希望“取消”您的PRINTF时,它可能对调试很有用。 To keep it displaying stuff, you'll just change the macro to为了保持显示内容,您只需将宏更改为

#define PRINTF if(true) printf

When ever you write当你写

PRINTF("this does nothing" ); PRINTF("这什么都不做");

it would textually replace it by它将在文本上替换为

if(false) printf("this does nothing" );

So basically it would do nothing as the condition is always false.所以基本上它什么都不做,因为条件总是假的。

As others already told you, this is a way to switch output on and off.正如其他人已经告诉您的那样,这是一种打开和关闭输出的方法。

However, it's a particulary error-prone way of doing so.但是,这是一种特别容易出错的方法。

Consider the following piece of code:考虑以下代码:

#include <stdio.h>

#define PRINTF if(false) printf

int main()
{
    bool x = true;
    int i = 0;

    if (x) {
        PRINTF("What ever");
    else
        i = 1;
    }
}

Can you guess what i will be?你能猜到i会是什么吗?

The answer is it will be 1 , because the compiler does not care about accidental wrong indentation, and the PRINTF macro expands as follows:答案是它会是1 ,因为编译器不关心意外的错误缩进, PRINTF宏扩展如下:

#include <stdio.h>

#define PRINTF if(false) printf

int main()
{
    bool x = true;
    int i = 0;

    if (x) {
        if(false) printf("What ever");
    else
        i = 1;
    }
}

Which is more easily read as:哪个更容易读为:

#include <stdio.h>

#define PRINTF if(false) printf

int main()
{
    bool x = true;
    int i = 0;

    if (x) {
        if(false)
            printf("What ever");
        else
            i = 1;
    }
}

Macros are evil! 宏是邪恶的!

You should use a different way of toggling output anyway.无论如何,您应该使用不同的方式来切换输出。 In fact, you should not blindly turn off logging messages in "release" versions (which seems to be this macro's intent), because this is where you will later need them most.事实上,你不应该在“发布”版本中盲目地关闭日志消息(这似乎是这个宏的意图),因为这是你以后最需要它们的地方。 On the other hand, only the most basic toy applications should unconditionally use direct standard output with printf or std::cout .另一方面,只有最基本的玩具应用程序才应该无条件地使用带有printfstd::cout直接标准输出。 You should instead operate on std::ostream references.您应该改为对std::ostream引用进行操作。 Search for related Stack Overflow questions about logging in C++.搜索有关登录 C++ 的相关 Stack Overflow 问题。 The topic is quite complex.这个话题相当复杂。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM