How do I know if this preprocessor macro exists?

I want to know how to find out if the __PRETTY_FUNCTION__ preprocessor macro __PRETTY_FUNCTION__ be used with this compiler (since it must be non-standard). How to check this in the header file? I want to do something like:

 #ifndef __PRETTY_FUNCTION__ #define __PRETTY_FUNCTION__ __func__ #endif 

But I assume that the preprocessor defines an in-place macro for each function, so I wonder if it makes sense for __PRETTY_FUNCTION__ (unlike __FILE__ or __LINE__ ) outside the function. Is this true or can I just use the code above? If not, how can I check it?

EDIT: I tried. __PRETTY_FUNCTION__ undefined outside the function (I did not check inside the class). So there must be another way.

EDIT2: Actually a simple hack would have to do this :):

 void Dummy() { #ifndef __PRETTY_FUNCTION__ #define __PRETTY_FUNCTION__ __func__ #endif } 

Another method is to check the compiler as suggested by others.

+4
source share
1 answer

You probably need to know which compiler you are using. For GCC (the GNU compiler compilation), you will probably experience:

 #ifdef __GNUG__ ...use __PRETTY_FUNCTION__ #endif 

You can check the compiler version if you know which one indicated this function, and you run the risk of compiling your code with an older version.

The GCC Guide (4.4.1) states:

In C, __PRETTY_FUNCTION__ is another name for __func__ . However, in C ++, __PRETTY_FUNCTION__ contains a signature of the type of the function, as well as its bare name. For example, this program:

  extern "C" { extern int printf (char *, ...); } class a { public: void sub (int i) { printf ("__FUNCTION__ = %s\n", __FUNCTION__); printf ("__PRETTY_FUNCTION__ = %s\n", __PRETTY_FUNCTION__); } }; int main (void) { a ax; ax.sub (0); return 0; } 

gives this result:

  __FUNCTION__ = sub __PRETTY_FUNCTION__ = void a::sub(int) 

These identifiers are not preprocessor macros. In GCC 3.3 and earlier, only in C, __FUNCTION__ and __PRETTY_FUNCTION__ considered as string literals; they can be used to initialize char arrays, and they can be combined with other string literals. GCC 3.4 and above consider them as variables, such as __func__ . In C ++, __FUNCTION__ and __PRETTY_ FUNCTION__ have always been variables.

+4
source

All Articles