問題描述
假設您的 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.
這篇關于__FILE__、__LINE__ 和 __FUNCTION__ 在 C++ 中的用法的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!