But when I replaced __LINE__ with __FUNCTION__.
Macro concat string literal "__FUNCTION__" and not actual function name.
Asked
Active
Viewed 294 times
2
gaurav bharadwaj
- 1,597
- 1
- 11
- 26
-
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
-
1No, 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
1 Answers
4
__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.
HolyBlackCat
- 63,700
- 7
- 105
- 170
-
-
3@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
-
@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"`. – PSkocik Jan 19 '21 at 20:15