I understand that the predefined identifier __func__
makes a function name available for use within the function. But when will i be needing it? One of the purpose can be for debugging. What are the other use cases?

- 19,754
- 5
- 44
- 74

- 61
- 6
-
11Debugging isn't a good enough use case? – jamesdlin May 28 '13 at 05:21
-
1It is. But just for knowledge purpose i want to know a few more. – akshay May 28 '13 at 05:24
-
I think that that's the only real purpose of the identifier. For any other purpose (e.g. reflection?), there's probably a better facility in the language. – May 28 '13 at 05:36
-
1Why do you think there are any more? You can't call a method by name in c, the name doesn't even exist after the code is compiled. – Kevin May 28 '13 at 05:58
3 Answers
Update: For clarity, since in (portable) C or C++ there is nothing you can do with the name of the current function as "string" (char
array), you're pretty much limited to writing that string somewhere - which can all be viewed as diagnostics or "debugging" purposes. Having that said, you might want to come up with some sort of usage for the function name. For example, declarative security, configurable logging (albeit that is "diagnostics" again, etc.).
In fact most descriptions and manuals on the web explicitly mention that the utility of __func__
and (__LINE__
and __FILE__
for that matter) is debugging and diagnostics.
Personally I think it is provided to help make "better" assert()
outputs possible.
You use that whenever you "programmatically" need to know the name of the current function. That pretty much excludes the debugger, where you have the name (or even context) of the current function by the very means of the debugger available already.
__func__
(or its older, non-standard predecessor __FUNCTION__
) is typically used together with the __LINE__
and maybe __FILE__
macros to provide trace or diagnostic output functionality, that automatically includes the current function (and file, line).
#define TRACE(s) \
do { fprintf(stderr, "%s:%d:%s: %s", __FILE__, __LINE__, __func__, (s)); \
} while (0)
You can then use it like this:
int my_func(char* arg1, int arg2)
{
TRACE("Doing stuff...");
/* ... */
}
For more information and examples see this Stackoverflow Q&A.
Even for such output-only purposes, the usability of __func__
is pretty limited, as it only includes the "unqualified and unadorned name" of the function. Such that for the above example it will only by "my_func".
In C/C++ that means that for overloaded functions, you will not be able to distinguish them by the value of __func__
alone. The class- and/or namespace-name, if any, is also not included.
Also compilers typically provide non-standard variations that do include the full function signature or other detail (GCC (see __PRETTY_FUNCTION__
), Microsoft C/C++ (see __FUNCDNAME__
, __FUNCSIG__
)).

- 1
- 1

- 47,778
- 10
- 99
- 143
-
...and all of your examples are actually **debugging** techniques. – SomeWittyUsername May 28 '13 at 06:51
-
@icepack Yes, of course. However, up to now (the time I initially wrote my answer) the mentioning of debugging was more in the context of actually using a _debugger_ not doing tracing and logging. And as I say in the second part of my answer there is really no other use possible (due to the limited nature of the information `__func__` provides). – Christian.K May 28 '13 at 06:55
-
Just a note: `__func__` is not a macro: it's a `static const` variable local to the function. – Morwenn May 28 '13 at 09:06
I've noticed that adding this macro along with __FILE__
and __LINE__
in my code greatly helps people who are reading the logs of my program. It also makes writing log functions a lot easier as I don't need to hard-code those values since the compiler takes care of that for me.
Remember that people may not have access to the source code of your application, in some cases only the log trace and the output will be available to them. It would be good if there was a transparent way for them to know what's going on.

- 7,310
- 1
- 33
- 67
If it is a constexpr
(which it should be, but at least in current GCC isn't, unluckily), it can be hashed and used to implement a very easy and efficient (compile-time, no runtime overhead) RTTI system.
It could also lend to a more usable version of std::type_info::hash_code
which neither requires RTTI to be linked nor has the pitfall of allowing different invocations to return different values.
The version supplied by the standard (as of C++11) explicitly gives no such guarantee, which makes it rather useless for anything except as a key in an associative container (such as std::map
).
Given the guarantee that different invocations (at least of the same binary, or at least of types that have the exact same definition), it would be much more usable, e.g. for serialization.
(Yes, I'm aware that hash functions have collisions. Yes, in theory that's an issue. However, given a reasonable amount of bits (say, 64) and reasonable amounts of keys (say, a few hundreds), they happen rarely enough as to not turn out being a problem in practice, and it's not something one can't verify.)

- 67,688
- 20
- 135
- 185