C ++条件编译 [英] C++ conditional compilation

查看:157
本文介绍了C ++条件编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码片段:

#ifdef DO_LOG
#define log(p) record(p)
#else
#define log(p)
#endif

void record(char *data){
.....
.....
}

现在如果我调用 log(hello world)在我的代码和 DO_LOG 没有定义,行会被编译,换句话说, up字符串hello world的内存?

Now if I call log("hello world") in my code and DO_LOG isn't defined, will the line be compiled, in other words will it eat up the memory for the string "hello world"?

在程序中有很多记录调用,它是内存敏感的,所以有什么其他方法有条件地编译,以便它只依赖于 #define DO_LOG

P.S. There are a lot of record calls in the program and it is memory sensitive, so is there any other way to conditionally compile so that it only depends on the #define DO_LOG?

推荐答案

这应该是微不足道的,通过检查生成的二进制文件来验证。

This should be trivial to verify for yourself by inspecting the resulting binary.

我会说no,因为表达式完全消失了,编译器将永远不会看到字符串(它被预处理器的宏扩展删除)。

I would say "no", since the expression totally goes away, the compiler will never see the string (it's removed by the preprocessor's macro expansion).

这篇关于C ++条件编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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