获取c++中当前方法的名称
有没有一种(标准化)方法可以使用 C++ 获取当前方法的名称?
使用 GNU GCC,您可以通过使用宏 __FUNCTION__
和 __PRETTY_FUNCTION__
(由 2 个下划线包围)来完成此操作,但是,这当然是不可移植的。 有没有办法在标准 C++ 中做到这一点或使其可移植?
Is there a (standardized) way to get the name of the current method using c++?
Using GNU GCC you can do this by using the macro __FUNCTION__
and __PRETTY_FUNCTION__
(surrounded by 2 underscores), however, this is of course non portable. Is there a way to do this in standard c++ or a way to make it portable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
__func__
标识符是 C99 标准的一部分,位于 C++0x 草案中。标识符
__func__
由编译器隐式声明,就好像以下内容位于每个函数体的开头一样:其中
function-name
是由实现自动提供的实现定义的字符串编译器。在对这些标准的支持可用(或更广泛可用)之前,您将陷入特定于编译器的困境。
The
__func__
identifier is part of the C99 standard and is in the C++0x draft.The identifier
__func__
is implicitly declared by the compiler as if the following were at the very start of each function body:where
function-name
is an implementation-defined string automatically provided by the compiler.Until support for those standards is available (or more widely available) you're stuck with compiler-specific gunk.
不……但是有些东西最终还是不标准的标准!
请参阅 http://msdn.microsoft.com/en- us/library/b0084kay(VS.80).aspx 概述了 ANSI 标准内容,然后是包含 FUNCTION 的 Microsoft
No...... but some things end up being non standard standards!
see http://msdn.microsoft.com/en-us/library/b0084kay(VS.80).aspx which outlines the ANSI standard stuff, and then microsofs which includes FUNCTION
不,C++ 标准不支持它。
No, it is not supported by c++ standard.
这就是所谓的“内省”,而 C++ 标准中没有这样的东西。 您应该使用外部库(我认为 boost 以某种方式支持它)。
但试着弄清楚使用它的理由是否真的是一个好的理由。
It's called "introspection" and there is no such a thing in c++ standards. You should use an external library (boost I think supports it somehow).
But try figure out if the reason for using it is really a good one.