有没有办法在 C++ 函数中获取函数名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/733056/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:58:57  来源:igfitidea点击:

Is there a way to get function name inside a C++ function?

c++macrosprofiling

提问by Canopus

I want to implement a function tracer, which would trace how much time a function is taking to execute. I have following class for the same:-

我想实现一个函数跟踪器,它可以跟踪一个函数执行所需的时间。我有以下相同的课程:-

class FuncTracer
{
    public:
        FuncTracer(LPCTSTR strFuncName_in)
        {
            m_strFuncName[0] = _T('
__FUNCTION__ for undecorated names
'); if( strFuncName_in || _T('
__FUNCDNAME__ for decorated names
') != strFuncName_in[0]) { _tcscpy(m_strFuncName,strFuncName_in); TCHAR strLog[MAX_PATH]; _stprintf(strLog,_T("Entering Func:- <%s>"),m_strFuncName); LOG(strLog) m_dwEnterTime = GetTickCount(); } } ~FuncTracer() { TCHAR strLog[MAX_PATH]; _stprintf(strLog,_T("Leaving Func:- <%s>, Time inside the func <%d> ms"),m_strFuncName, GetTickCount()-m_dwEnterTime); LOG(strLog) } private: TCHAR m_strFuncName[MAX_PATH]; DWORD m_dwEnterTime; }; void TestClass::TestFunction() { // I want to avoid writing the function name maually.. // Is there any macro (__LINE__)or some other way to // get the function name inside a function ?? FuncTracer(_T("TestClass::TestFunction")); /* * Rest of the function code. */ }

I want to know if there is any way to get the name of the function from inside of a function? Basically I want the users of my class to simply create an object the same. They may not pass the function name.

我想知道是否有任何方法可以从函数内部获取函数的名称?基本上我希望我的班级的用户简单地创建一个相同的对象。他们可能不会传递函数名称。

采纳答案by sharptooth

VC++ has

VC++有

#define ALLOC_LOGGER FuncTracer ____tracer( __FUNCTION__ );

and

##代码##

And you can write a macro that will itself allocate an object and pass the name-yelding macro inside the constructor. Smth like

并且您可以编写一个宏,它本身会分配一个对象并在构造函数中传递命名宏。喜欢

##代码##

回答by bk1e

C99 has __func__, but for C++ this will be compiler specific. On the plus side, some of the compiler-specific versions provide additional type information, which is particularly nice when you're tracing inside a templatized function/class.

C99 有__func__,但对于 C++ 这将是特定于编译器的。从好的方面来说,一些特定于编译器的版本提供了额外的类型信息,这在您在模板化的函数/类中进行跟踪时特别好。

  • MSVC: __FUNCTION__, __FUNCDNAME__, __FUNCSIG__
  • GCC: __func__, __FUNCTION__, __PRETTY_FUNCTION__
  • MSVC: __FUNCTION__, __FUNCDNAME__,__FUNCSIG__
  • GCC__func____FUNCTION____PRETTY_FUNCTION__

Boost library has defined macro BOOST_CURRENT_FUNCTIONfor most C++ compilers in header boost/current_function.hpp. If the compiler is too old to support this, the result will be "(unknown)".

Boost 库BOOST_CURRENT_FUNCTION在头文件boost/current_function.hpp 中为大多数 C++ 编译器定义了宏。如果编译器太旧而无法支持这一点,结果将是“(未知)”。

回答by David Z

I was going to say I didn't know of any such thing but then I saw the other answers...

我想说我不知道​​任何这样的事情,但后来我看到了其他答案......

It might interest you to know that an execution profiler (like gprof) does exactly what you're asking about - it tracks the amount of time spent executing each function. A profiler basically works by recording the instruction pointer (IP), the address of the currently executing instruction, every 10ms or so. After the program is done running, you invoke a postprocessor that examines the list of IPs and the program, and converts those addresses into function names. So I'd suggest just using the instruction pointer, rather than the function name, both because it's easier to code and because it's more efficient to work with a single number than with a string.

您可能有兴趣知道执行分析器(如gprof)完全按照您的要求执行 - 它跟踪执行每个函数所花费的时间。分析器的工作原理是每 10 毫秒左右记录一次指令指针 (IP),即当前执行指令的地址。程序运行完毕后,您调用一个后处理器来检查 IP 和程序的列表,并将这些地址转换为函数名称。所以我建议只使用指令指针,而不是函数名称,因为它更容易编码,并且因为使用单个数字比使用字符串更有效。