2

Simlar question with LINE

But when I replaced __LINE__ with __FUNCTION__. Macro concat string literal "__FUNCTION__" and not actual function name.

gaurav bharadwaj
  • 1,669
  • 1
  • 12
  • 29
  • Please ask the actual question you have in *this* question. Linking to another, putatively similar question does not suffice. SO questions should stand on their own. – John Bollinger Jan 19 '21 at 20:00
  • 1
    [C11 `__func__`](http://port70.net/~nsz/c/c11/n1570.html#6.4.2.2) is not a macro. The preprocessor does not care about it. The same happens with gcc's `__FUNCTION__` extension ( see https://gcc.gnu.org/onlinedocs/gcc/Function-Names.html ). – pmg Jan 19 '21 at 20:00
  • @pmg any way to stringizing(##) functioname through macro. – gaurav bharadwaj Jan 19 '21 at 20:03
  • 1
    No, not directly. The pre-processor does not have any way to get that information. Unless, of course, you give the information manually... `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* – pmg Jan 19 '21 at 20:05
  • 1
    `__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. – Eric Postpischil Jan 19 '21 at 23:28

2 Answers2

5

__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.

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
  • I want to it at preprocessor time. Any way? – gaurav bharadwaj Jan 19 '21 at 20:03
  • 4
    @gauravbharadwaj Nope, can't do that. Preprocessor is a simple thing, it doesn't know what functions are. – HolyBlackCat Jan 19 '21 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"`. – Petr Skocik Jan 19 '21 at 20:15
0

This will keep __FUNCTION__ as a string macro:

#pragma iso_9899_1999
dargaud
  • 2,431
  • 2
  • 26
  • 39
  • Documentation? What compiler(s) does this apply to? – Andrew Henle Mar 30 '23 at 16:19
  • 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. – dargaud Mar 30 '23 at 16:23