Visual C++相当于 __FILE__ 、 __LINE__ 和 __PRETTY_FUNCTION__
GCC 编译器为我提供了以下宏:
__FILE__
,以便我可以打印出文件名+目录。__LINE__
这样我就可以打印出我正在打印的行号。__PRETTY_FUNCTION__
这样我就可以打印出漂亮的函数名称
Visual C++ 是否有与这些宏等效的功能?附带问题是,这些是 C++ 编译器的标准吗?
GCC compiler gives me the following macros:
__FILE__
so that I can print out the file name + directory.__LINE__
so that I can print out the line number of where I'm printing from.__PRETTY_FUNCTION__
so that I can print out the pretty function name
Does Visual C++ have the equivalent of these macros? A side question is, are these standard for C++ compilers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
在 VS2008 中,这个:
将打印这个:(
行号是“错误的”,因为我的文件顶部确实有一些额外的东西。)
In VS2008, this:
will print this:
(The line number is "wrong" since there was really some extra stuff at the top of my file.)
__FILE__
和__LINE__
是标准的,我相当确定 Microsoft 编译器基本上一直都有它们。__PRETTY_FUNCTION__
是 gcc 功能。__FILE__
and__LINE__
are standard, and I'm rather certain Microsoft compilers have essentially always had them.__PRETTY_FUNCTION__
is a gcc feature.为了获得当前函数名称的更多可移植性,您可以尝试 BOOST_CURRENT_FUNCTION< /a>.
For more portability in getting the current function name, you can try BOOST_CURRENT_FUNCTION.
是的,Visual C++ 有它们或等效的东西。请参阅此处的回复:
__PRETTY_FUNCTION__、__FUNCTION__、__func__ 之间有什么区别?
function-func/4384860#4384860
另请注意,尽管使用了大写字母,但它们不是宏。它们是变量。
Yes Visual C++ has them or an equivalent. See the responses here:
What's the difference between __PRETTY_FUNCTION__, __FUNCTION__, __func__?
function-func/4384860#4384860
Also note that despite the upper case used, they aren't macros. They're variables.
我知道MSVC提供了
__FILE__
和__LINE__
,它们都是标准宏。他们还提供 __FUNCTION__ ,我相信这就是您正在寻找的,I know that MSVC offers
__FILE__
and__LINE__
, both of which are Standard macros. They also offer__FUNCTION__
, which I believe is what you're looking for,__FILE__
和__LINE__
是自 C89 以来的标准。__PRETTY_FUNCTION__
是一种 GCCism。__func__
是一种 C99ism(与 GCCism 不同),很可能在 Visual C++ 中可用;它与__PRETTY_FUNCTION__
并不完全相同,但对于您的目的而言,它可能足够接近。__FILE__
and__LINE__
are standard since C89.__PRETTY_FUNCTION__
is a GCCism.__func__
is a C99ism which (unlike GCCisms) may well be available in Visual C++; it is not precisely the same as__PRETTY_FUNCTION__
but it may be close enough for your purposes.是的,Microsoft Visual Studio 有
__FILE__
和__LINE__
。 这里有更多 MSVC 宏。两者都是 ANSI C++。
MSVC 具有
__FUNCTION__
,这是 Microsoft 特定的。Yes, Microsoft Visual Studio has
__FILE__
and__LINE__
. Here are more MSVC macros.Both are ANSI C++.
MSVC has
__FUNCTION__
, which is Microsoft-specific.使用c++14 与
constexpr
你可以使用这个:<代码>WHERE宏。基于以下内容的使用:
__PRETTY_FUNCTION__
__LINE__
用法示例:
完整&运行示例
请参阅:
Using c++14 with
constexpr
you can use this:WHERE
macro.Based on usage of:
__PRETTY_FUNCTION__
__LINE__
Example usage:
Full & running example here
See: