简体   繁体   English

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

[英]Getting the name of the current method in c++

Is there a (standardized) way to get the name of the current method using c++? 是否有(标准化)方法使用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. 使用GNU GCC可以使用宏__FUNCTION____PRETTY_FUNCTION__ (由2个下划线包围)来实现,但是,这当然是不可移植的。 Is there a way to do this in standard c++ or a way to make it portable? 有没有办法在标准c ++中实现这一点或使其可移植?

The __func__ identifier is part of the C99 standard and is in the C++0x draft. __func__标识符是C99标准的一部分,位于C ++ 0x草案中。

The identifier __func__ is implicitly declared by the compiler as if the following were at the very start of each function body: 标识符__func__由编译器隐式声明,就好像以下是每个函数体的开头一样:

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

where function-name is an implementation-defined string automatically provided by the compiler. 其中function-name是由编译器自动提供的实现定义字符串。

Until support for those standards is available (or more widely available) you're stuck with compiler-specific gunk. 在获得对这些标准的支持(或更广泛可用)之前,您会遇到特定于编译器的垃圾。

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 请参阅http://msdn.microsoft.com/en-us/library/b0084kay(VS.80).aspx ,其中概述了ANSI标准的内容,然后是包含FUNCTION的 microsofs

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

It's called "introspection" and there is no such a thing in c++ standards. 它被称为“内省”,在c ++标准中没有这样的东西。 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. 但试着弄清楚使用它的原因是否真的很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM