C++,获取函数名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3078851/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++, get the name of the function
提问by Max Frai
In C++, is there a way to get the function signature/name from it's pointer like this?
在 C++ 中,有没有办法从这样的指针中获取函数签名/名称?
void test(float data) {}
cout << typeid(&test).name();
I want to use this data for logging.
我想使用这些数据进行日志记录。
回答by Eric Leschinski
C++, get the name of the calling function via a pointer:
C++,通过指针获取调用函数的名称:
Option 1: roll your own function name recorder
选项 1:滚动您自己的函数名称记录器
If you want to resolve a "pointer to a function" to a "function name", you will need to create your own lookup table of all available functions, then compare your pointer address to the key in the lookup table, and return the name.
如果要将“指向函数的指针”解析为“函数名称”,则需要创建自己的所有可用函数的查找表,然后将指针地址与查找表中的键进行比较,并返回名称.
An implementation described here: https://stackoverflow.com/a/8752173/445131
此处描述的实现:https: //stackoverflow.com/a/8752173/445131
Option 2: Use __func__
选项 2:使用 __func__
GCC provides this magic variable that holds the name of the current function, as a string. It is part of the C99 standard:
GCC 提供了这个魔法变量,它以字符串的形式保存当前函数的名称。它是 C99 标准的一部分:
#include <iostream>
using namespace std;
void foobar_function(){
cout << "the name of this function is: " << __func__ << endl;
}
int main(int argc, char** argv) {
cout << "the name of this function is: " << __func__ << endl;
foobar_function();
return 0;
}
Output:
输出:
the name of this function is: main
the name of this function is: foobar_function
Notes:
笔记:
__FUNCTION__is another name for __func__. Older versions of GCC recognize only this name. However, it is not standardized. If maximum portability is required, we recommend you use __func__, but provide a fallback definition with the preprocessor, to define it if it is undefined:
__FUNCTION__是 的另一个名称__func__。旧版本的 GCC 仅识别此名称。但是,它不是标准化的。如果需要最大的可移植性,我们建议您使用__func__,但在预处理器中提供回退定义,如果未定义,则对其进行定义:
#if __STDC_VERSION__ < 199901L
# if __GNUC__ >= 2
# define __func__ __FUNCTION__
# else
# define __func__ "<unknown>"
# endif
#endif
Source: http://gcc.gnu.org/onlinedocs/gcc/Function-Names.html
回答by Hyman Shainsky
If you just want to log the current function name, most of the compilers have __FUNCTION__macro, which will give you the current function name at compile time.
如果您只想记录当前函数名,大多数编译器都有__FUNCTION__宏,它会在编译时为您提供当前函数名。
You may also look for stack walking techniques (here is an example for Windows), which can provide you more information about the current call stack and function names at runtime.
您还可以寻找堆栈遍历技术(这里是Windows 的示例),它可以在运行时为您提供有关当前调用堆栈和函数名称的更多信息。
回答by valdo
There's no way you can get the name of the function. Just because it doesn't reside inside the executable. It vanishes completely after your code is compiled & linked.
您无法获得函数的名称。仅仅因为它不驻留在可执行文件中。在您的代码编译和链接后,它会完全消失。
You may try renaming your functions/variables, and your executable will be the same (apart from mutable things the compiler may put, like build date/time, debug information ID, etc.)
您可以尝试重命名您的函数/变量,并且您的可执行文件将是相同的(除了编译器可能放置的可变内容,如构建日期/时间、调试信息 ID 等)
Also try to open the executable file with some editor and look for the function name. Most likely you'll not find it.
也尝试用一些编辑器打开可执行文件并查找函数名称。很可能你不会找到它。
You may however put some programmatic "decorations" that'll help you discover your function name at the runtime.
但是,您可以放置一些程序化的“装饰”,以帮助您在运行时发现您的函数名称。
回答by David Rodríguez - dribeas
You cannot get the name of the function in C++, but you can print the pointer and later check the binary (if not stripped) for the function name. The signature can be printed exactly as you are doing, just that the type name is not really 'human readable'. Check your compiler documentation for what the output of your code means. In g++ the output will be PFvfE, which I don't understand completely, but identifies a pointer (P) to a function (F) returning void (v) and taking a float (f) as single argument. Don't ask me what the Eis...
您无法在 C++ 中获得函数的名称,但您可以打印指针,然后检查函数名称的二进制文件(如果未剥离)。签名可以完全按照您的方式打印,只是类型名称不是真正的“人类可读”。检查您的编译器文档,了解代码输出的含义。在 g++ 中,输出将是PFvfE,我不完全理解,但标识了一个指向P函数 ( F)的指针 ( ),返回 void ( v) 并将浮点 ( f) 作为单个参数。不要问我这E是什么...
(I don't have time to check the docs now, I just played with a sample program to guess that: print different function signatures)
(我现在没有时间检查文档,我只是玩了一个示例程序来猜测:打印不同的函数签名)
回答by InsertNickHere
I'm not 100% sure, but this seems to me like reflection (Java), and C++ does not support things like this. It may be that I just don't know,but i havent seen this for C++ yet.
我不是 100% 确定,但这在我看来就像反射(Java),而 C++ 不支持这样的事情。可能我只是不知道,但我还没有在 C++ 中看到这个。

