使用预处理器取消 std::cout 代码行
可以使用#define printf
删除对printf()
的所有调用。如果我有很多调试打印,例如 std::cout << x <<结束;
?如何使用预处理器快速关闭单个文件中的 cout <<
语句?
One can remove all calls to printf()
using #define printf
. What if I have a lot of debug prints like std::cout << x << endl;
? How can I quickly switch off cout <<
statements in a single file using preprocessor?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
正如“放松”已经说过的,快速解决方案是无所事事的流。不过,还有更好的实现:
您仍然对
std::cout
有一个小问题,因为它是三个标记的序列,并且您确实不想重新定义std
或分别cout
。一个简单的解决方案是As "unwind" already said, the quick solution is a do-nothing stream. There are better implementations though:
You still have a slight issue with
std::cout
since that's a sequence of three tokens, and you really don't want to redefinestd
orcout
individually. A simple solution is作为一般原则,应避免记录到标准输出 - 更好地记录到日志文件,然后您可以使用标准配置工具来更改日志级别,或完全关闭它。
只是我的 0.02 美元......
As a general principle logging to stdout should be avoided - far better to log to a logfile, and then you can use standard configuration tools to change log levels, or turn it off altogether.
Just my $0.02.....
将调试输出语句替换为如下内容:
然后您可以相应地定义宏:
Substitute your debug output statements with something like this:
Then you can define macros accordingly:
定义这个宏:
这个宏的优点是编译器优化
Define this macro :
This macro advantage is in compiler optimization
如果您正在寻找快速删除调试语句的方法,NullStream 可能是一个很好的解决方案。不过,我建议创建您自己的调试类,当需要更多调试功能时,可以根据需要扩展该类:
这是一个简单的设置,可以执行您最初想要的操作,而且它还有一个额外的好处,可以让您添加功能,例如调试级别等。
更新:
现在,由于操纵器是作为函数实现的,如果您也想接受操纵器(endl),您可以添加:
对于所有操纵器类型(这样您就不必重载所有操纵器类型):
请小心最后一个,因为它也接受常规函数指针。
NullStream can be a good solution if you are looking for something quick that removes debug statements. However I would recommend creating your own class for debugging, that can be expanded as needed when more debug functionality is required:
This is a simple setup that can do what you want initially, plus it has the added benefit of letting you add functionality such as debug levels etc..
Update:
Now since manipulators are implemented as functions, if you want to accept manipulators as well (endl) you can add:
And for all manipulator types (So that you don't have to overload for all manipulator types):
Be careful with this last one, because that will also accept regular functions pointers.
您可能可以进行预处理器修改,定义一个新的类似流的类,并使用名为
cerr
的实例,但它什么也不做。如果你真的很幸运,编译器会发现该函数不执行任何操作,并优化对operator<<()
的调用。像这样的东西
虽然很黑客,但最好(远)更好地浏览您的源代码并添加对日志记录的适当支持。
You can probably do a preprocessor hack that defines a new stream-like class, with an instance named
cerr
, that just does nothing. If you're really lucky, the compiler will see that the function does nothing, and optimize the calls tooperator<<()
out.Something like
This is quite the hack though, it's (far) better to go through your source and add proper support for logging.
定义一个宏来替换
cout
不是您应该上传到 VCS 的东西,但如果您只是在调试期间临时执行此操作,我认为它发挥了作用。因此,您只需将cout
替换为ostream(0)
这样,它就可以与
std::cout
和普通cout 一起使用
,并且当包含
时,ostream
可用。写入ostream(0)
是一个空操作。完成flush
函数调用,以便您获得对其的非常量引用(因此它也绑定到用于输出的非成员
等)。由于其类型为operator<<
>std::stringostream
,因此它的行为应与cout
完全相同。Defining a macro that replaces
cout
is not something you should upload to your VCS, but if you just do it temporarily during debugging, I think it serves its place. So you can just replacecout
byostream(0)
likeThis way, it works with both
std::cout
and plaincout
, andostream
is available when including<iostream>
. Writing into aostream(0)
is a no-op. Theflush
function call is done so that you get a non-const reference to it (so it also binds to non-memberoperator<<
that's used for outputtingstd::string
and others). As its type isostream
, it should behave exactly likecout
.