8

In C/C++, we have the __FUNCTION__ macro which is replaced with a string, holding the name of the current function. But what if I want the function's identifier? That is, not a string, but something I could use as a token to create other identifiers, e.g., if we have

#define MAGIC /* ... */

#define MORE_MAGIC MAGIC ## _bar

void foo() {
    printf("%s\n",__FUNCTION__);
    MORE_MAGIC();
}

void foo_bar() {
    printf("%s\n",__FUNCTION__);
}

void baz() {
    printf("%s\n",__FUNCTION__);
    MORE_MAGIC();
}

void baz_bar() {
    printf("%s\n",__FUNCTION__);
}

int main() {
    foo();
}

should print

foo
foo_bar
baz
baz_bar

Notes:

  • I'm interested in preprocessing-time only.
  • I would rather not replace my function definitions with a preprocessor call - although I know that would probably work.
AstroCB
  • 12,337
  • 20
  • 57
  • 73
einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • 1
    C and C++ doesn't have [introspection](http://en.wikipedia.org/wiki/Introspection_%28computer_science%29), so it's not possible. You *could* have a hand-made mapping between function name and function pointer, but it's not possible to automate. – Some programmer dude Mar 11 '14 at 08:47
  • 6
    @JoachimPileborg: I believe the OP is asking about compile-time magic, rather than runtime magic (i.e. introspection). – Oliver Charlesworth Mar 11 '14 at 08:48
  • 3
    @JoachimPileborg I agree that automation in general would be difficult if not impossible, but it could be easy for special cases depending on @einpoklum's tolerance for nasty macros. You could define a DEF_FUNC macro that expands to the function prototype as well as a registration of the function in some sort of function pointer table. I.e. `DEF_FUNC(f) void f() { REGISTER(f, __FUNCTION__) ` – Adam Mar 11 '14 at 08:53
  • Normally the c/c++ preprocessor doesn't do string manipulation, but if you've got more complex cases than @JoachimPileborg answer below easilty supports and are willing to venture into weird stuff there's things like this: http://insanecoding.blogspot.se/2011/10/stronger-cc-preprocesser.html – dutt Mar 11 '14 at 08:58
  • I *had* to ask: why do you think you need this? – The Paramagnetic Croissant Nov 02 '14 at 18:55

2 Answers2

3

Unfortunately you can't. Because you can not unstringify a macro[1].

In other words, you can not remove quotes around the string that generated by __FUNCTION__ and contact it by _bar.

Community
  • 1
  • 1
masoud
  • 55,379
  • 16
  • 141
  • 208
  • 1
    I agree. In a similar situation, where I had to write a C++ wrapper, I simply wrote a **perl script** which processed a `foo.cpp.tmpl` into the final `foo.cpp` at compile time. – Flovdis Mar 11 '14 at 09:13
  • 2
    Yep, `__FUNCTION__` is a string literal and there's not a thing you can do about it. – David Schwartz Mar 11 '14 at 09:17
  • I did not say I want to un-stringify __FUNCTION__, I was wondering whether compilers, or pre-processors, offer the functionality I'm interested in, directly. – einpoklum Mar 11 '14 at 09:33
  • @einpoklum: I understand what you mean. One possibility in the standard C++ was un-stringifying `__FUNCTION__` which is not possible. Maybe there is a compiler magic that do it, but it's not a standard and portable way. – masoud Mar 11 '14 at 09:36
2

If it's compile-time you want, and for a simple case like your, it might be possible with preprocessor macros and the concatenation operator ##. Maybe something like

#define MORE_MAGIC(f) f##_bar

...

void foo_bar()
{
}

void foo()
{
    MORE_MAGIC(foo)();
}

It's not possible to get the name foo automatically though, it has to be explicitly named in the macro "call".

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621