__FILE__、__LINE__ 和 __FUNCTION__ 在 C++ 中的用法
假设您的 C++ 编译器支持它们,是否有任何特殊原因不使用 __FILE__
、__LINE__
和 __FUNCTION__代码> 用于记录和调试目的?
Presuming that your C++ compiler supports them, is there any particular reason not to use __FILE__
, __LINE__
and __FUNCTION__
for logging and debugging purposes?
我主要关心的是向用户提供误导性数据――例如,报告不正确的行号或由于优化而导致的函数――或因此导致性能下降.
I'm primarily concerned with giving the user misleading data―for example, reporting the incorrect line number or function as a result of optimization―or taking a performance hit as a result.
基本上,我可以相信 __FILE__
、__LINE__
和 __FUNCTION__
会总是做正确的事吗?>
Basically, can I trust __FILE__
, __LINE__
and __FUNCTION__
to always do the right thing?
推荐答案
__FUNCTION__
是非标准的,__func__
存在于 C99/C++11 中.其他的(__LINE__
和 __FILE__
)都很好.
__FUNCTION__
is non standard, __func__
exists in C99 / C++11. The others (__LINE__
and __FILE__
) are just fine.
它总是会报告正确的文件和行(如果您选择使用 __FUNCTION__
/__func__
,则会报告正确的文件和行).优化是一个非因素,因为它是一个编译时宏扩展;它绝不会以任何方式影响性能.
It will always report the right file and line (and function if you choose to use __FUNCTION__
/__func__
). Optimization is a non-factor since it is a compile time macro expansion; it will never affect performance in any way.
相关文章