获取c++中当前方法的名称

发布于 2024-07-08 06:26:12 字数 191 浏览 4 评论 0原文

有没有一种(标准化)方法可以使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

俯瞰星空 2024-07-15 06:26:12

__func__ 标识符是 C99 标准的一部分,位于 C++0x 草案中。

标识符 __func__ 由编译器隐式声明,就好像以下内容位于每个函数体的开头一样:

static const char __func__[] = "function-name";

其中 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:

static const char __func__[] = "function-name";

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.

锦上情书 2024-07-15 06:26:12

不……但是有些东西最终还是不标准的标准!

请参阅 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

小伙你站住 2024-07-15 06:26:12

不,C++ 标准不支持它。

No, it is not supported by c++ standard.

无所的.畏惧 2024-07-15 06:26:12

这就是所谓的“内省”,而 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文