But when I replaced __LINE__
with __FUNCTION__
.
Macro concat string literal "__FUNCTION__
" and not actual function name.
2 Answers
__FUNCTION__
is not a macro, it's an implicitly declared static array. The same is true for __func__
, __PRETTY_FUNCTION__
, etc.
Thus #
can't work on it. If you want to concatenate something to it, you'll have to do that at runtime, or with constexpr
.
-
-
4@gauravbharadwaj Nope, can't do that. Preprocessor is a simple thing, it doesn't know what functions are. Commented Jan 19, 2021 at 20:03
-
1@gauravbharadwaj The preprocessor is (at least conceptually) decoupled from the parser and it operates just on directives and token streams. As such it lacks the parsing capability to recognize that after
int (*func(int A, int B))(int C) {
,__func__
should expand to"func"
. Commented Jan 19, 2021 at 20:15
This will keep __FUNCTION__
as a string macro:
#pragma iso_9899_1999
-
-
You are right, I just tested it on gcc and it has no impact. I usually use it with clang. As for documentation, I don't know where I found it long ago, sorry. Google does not seem much help.– dargaudCommented Mar 30, 2023 at 16:23
__func__
is not a macro. The preprocessor does not care about it. The same happens with gcc's__FUNCTION__
extension ( see gcc.gnu.org/onlinedocs/gcc/Function-Names.html ).int fx(void) { #define CURRFUNC "fx" ... } #undef CURRFUNC
... but, even with this, you can only use CURRFUNC inside the function. Note that this is a bad idea and I do not reccomend you try anyhting with it__func__
is already a string, although it is a named array rather than a string literal. What do you want to do with it? It may already be suited for some uses as a string, so there would be no need to stringize it.