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

后端 未结 4 924
后悔当初
后悔当初 2020-12-01 06:22

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 FuncTrace         


        
4条回答
  •  臣服心动
    2020-12-01 06:58

    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.

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

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

提交回复
热议问题