Sampath
Sampath

Reputation: 1202

Usage of __FUNCTION__, __PRETTY_FUNCTION__ in non GCC compiler

I'm using __FUNCTION__, __PRETTY_FUNCTION__ macros in my code and learned that the code needs to compile under compilers other than GCC. Are these macros part of the standard? or GCC extension? Should I use __func__ instead?

Upvotes: 2

Views: 6755

Answers (2)

eerorika
eerorika

Reputation: 238461

Are these macros part of the standard?

No.

Should I use __func__ instead?

__func__ is standard (and is not a macro). If a compiler does not support the others, you can fall back to this - as long as the compiler supports a recent standard. On an older compiler, you'll have to use a compiler specific macro.

Note that the different macros may provide varying levels of information beyond the function name. __PRETTY_FUNCTION__ in particular provides useful additional information (namespace qualified, full signature, template arguments).

If you want the prettiest function name a compiler has to offer (if any), you need to detect the compiler with pre defined macros and research what each compiler version support. Or, if you don't like re-implementing what others have already done for you, you could use BOOST_CURRENT_FUNCTION.

Upvotes: 3

wilx
wilx

Reputation: 18268

  • __FUNCTION__ is available pretty much everywhere but it is just a simple function name
  • __PRETTY_FUNCTION__ with GCC compatible compilers like GCC, Clang and Intel's compiler and even with compilers like Sun Studio 12's one.
  • beware, __func__ is not a macro
  • Visual Studio C++ compiler has __FUNCSIG__ (you probably want this) and __FUNCDNAME__ (less useful as it is the full decorated name)

Upvotes: 4

Related Questions