How to get the function name while in a function for debug strings?

前端 未结 5 1477
故里飘歌
故里飘歌 2021-02-01 20:35

I want to output the function name each time it is called, I can easily copy and paste the function name, however I wondered if there was a shortcut that would do the job for me

相关标签:
5条回答
  • 2021-02-01 20:51

    I see from your example that you are using Qt. In which case your best bet is to use Q_FUNC_INFO found in <QGlobal>. Here's the description:

    Expands to a string that describe the function the macro resides in. How this string looks more specifically is compiler dependent. With GNU GCC it is typically the function signature, while with other compilers it might be the line and column number.

    0 讨论(0)
  • 2021-02-01 20:56

    __func__ is c99 ( which does in turn mean it might not work with visual studio - but hey it's standard :o) )

    __FUNCTION__ works pretty much everywhere

    __PRETTY_FUNCTION__ is gnu specific and returns the full qualified name with (namespaces?), classname, returntype, functionname, parameterslist

    0 讨论(0)
  • 2021-02-01 21:00

    If you check out boost there is a macro BOOST_CURRENT_FUNCTION that is portable across platforms. In the C99 standard there is a compiler variable __func__ that has the desired effect. I believe has been accepted into the C++0x standard. A reasonable number of compilers will already support this.

    0 讨论(0)
  • 2021-02-01 21:08

    "__FUNCTION__" is supported by both MSVC and GCC and should give you the information you need.

    0 讨论(0)
  • 2021-02-01 21:08

    If you are using gcc you may find __PRETTY_FUNCTION__ more to your liking.

    0 讨论(0)
提交回复
热议问题