2

I am seeing some really weird errors in a complex file, and I suspect I have some include with namespaces left open, or something similar. I cannot compile some file to an executable state.

To figure out what is the problem, I wanted to print the current namespace. I thought I would be fine using something like __func__ (available since C++11) or __PRETTY_FUNCTION__, but I cannot get to print it.

I cannot include it directly in a #pragma message(__func__) ("warning: expected a string after #pragma message"), and stringifying doesn't help, so namely, if I use this code:

#define STR_(x) #x
#define STR(x) STR_(x)

void fooMessage() {
#pragma message(STR(__func__))
}

I cannot get to print anything but the string "__func__". It seems to work for __LINE__, but it's not useful for what I want to do...

Is there a way to have a pragma message to print the complete function name (or to know the current namespace) from clang and/or gcc compiler?

Some things I have tried are available here

Antonio
  • 19,451
  • 13
  • 99
  • 197
  • Unrelated to your problem, but symbols with a leading underscore followed by an upper-case letter (like `_STR`) [are reserved in all scopes](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier). – Some programmer dude Dec 19 '17 at 19:22
  • 3
    A problem with `__func__` is that e.g. `__LINE__` is handled like a preprocessor macro that expands to the current line, and `__func__` is a *variable* that is handled after after the preprocessor is finished. – Some programmer dude Dec 19 '17 at 19:24
  • Is #error handled any different than #pragma message as far as this expansion is concerned? – SoronelHaetir Dec 19 '17 at 19:28
  • @SoronelHaetir `#error` looks even worse http://coliru.stacked-crooked.com/a/81bfd16e9534b63a (cannot convert even `__LINE__`) – Antonio Dec 19 '17 at 20:57

0 Answers0