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. Is there a way to do this in standard c++ or a way to make it portable?
使用GNU GCC可以使用宏__FUNCTION__和__PRETTY_FUNCTION__(由2个下划线包围)来实现,但是,这当然是不可移植的。有没有办法在标准c ++中实现这一点或者使其可移植?
4 个解决方案
#1
26
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.
在获得对这些标准的支持(或更广泛可用)之前,您会遇到特定于编译器的垃圾。
#2
1
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
#3
0
No, it is not supported by c++ standard.
不,c ++标准不支持它。
#4
0
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.
它被称为“内省”,在c ++标准中没有这样的东西。你应该使用一个外部库(我想以某种方式支持它)。但试着弄清楚使用它的原因是否真的很好。
#1
26
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.
在获得对这些标准的支持(或更广泛可用)之前,您会遇到特定于编译器的垃圾。
#2
1
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
#3
0
No, it is not supported by c++ standard.
不,c ++标准不支持它。
#4
0
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.
它被称为“内省”,在c ++标准中没有这样的东西。你应该使用一个外部库(我想以某种方式支持它)。但试着弄清楚使用它的原因是否真的很好。