Presenting the Problem
One of the frustrating phases of debugging an application consists of checking whether certain functions ever get called. A naïve solution to this problem consists of adding a cout
if you're using Clike this:
//..more code here
In a typical project that contains thousands of functions adding such an output statement to each function is a pain. Instead, you need a mechanism that will automate this process.
Retrieving a Function's Name
As a C++ programmer, you must have come across the built-in macros __TIME__ , __FILE__ and __DATE__ that are translated at compile time into strings containing the compilation time, the name of the translation unit being processed and the current date, respectively.
The latest ISO C standard, commonly known as C99, added to these intrinsic macros another useful macro-like expression called __func__ which reports the unadorned (i.e., non-mangled) name of the function from which it is accessed. Notice that __func__ isn't a macro because the preprocessor knows nothing about functions. Instead, it's implemented as an implicitly-declared const char array:
static const char __func__ = "function-name";
Where "function name" is the actual name of the function. In order to activate this facility, some compilers require that you use certain compilation flags. Check the documentation of your compiler for specific details.
Using this facility, you can automate much of the drudgery associated with displaying a function's name manually, as this rewritten version of the previous example shows:
It's worth noting that the official C99 standard defines the identifier __func__
for this purpose. However, ISO C++ doesn't fully-support all C99 extensions yet. Therefore, most vendors have settled for __FUNCTION__
is usually implemented as a macro
defined as __func__
. In this solution, I adhere to __FUNCTION__
because it's the most widely-supported name. You can find the exact token used by four leading compilers here:
- In Visual Studio 2005 it's on by default but it cannot be used with the /EP and /P compilation options. Notice that in this IDE the token __func__ isn't recognized. Use __FUNCTION__ instead.
- Comeau users should use the token __FUNCTION__ instead of __func__.
- C++ BuilderX uses a slightly different name for this identifier: __FUNC__.
- GCC 3.0 and higher support __func__ and __FUNCTION__.
Once the mechanism for obtaining the current function's name is automated, you can define a function that displays the name of any function:
void show_name(const char * name)
show_name(__FUNCTION__); //output: myfunc
show_name(__FUNCTION__); //output: foo
Recall that __FUNCTION__
is initialized immediately after the opening brace of a function. Therefore, foo()
can safely use it in the argument list of show_name()
without overriding it.