Is it possible to use __func__ in gcc 3.3+ the old way? (C++)

c++, gcc

Solution

Since `__FUNCTION__` and `__func__` is a predefined identifier and not a string literal, you cannot use it in preprocessor string literal concatenation. But you can use it in `printf` formatting. Also note the use of `##args` instead of `__VA_ARGS__` to use GNU style variadic macro arguments to work around the issue with the comma between `__func__` and possibly zero args.

#define DEBUG_WARNING(fmt, args...) \
  printf(">WARNING: %s() " fmt "\n", __func__, ##args)

Problem

With gcc versions before 3.3 and with the MS compiler I use the following macro: ``` DEBUG_WARNING(...) printf(">WARNING: "__FUNCTION__"() " __VA_ARGS__); ``` Use: ``` DEBUG_WARNING("someFunction returned %d", ret); ``` Output: ``` >WARNING: Class::FunctionName() someFunction returned -1 ``` Its extremely handy when we have lots of systems, all sending output. Its a single line macro, that allows us to filter the output accordingly. Small code, big use, happy me. As the `__FUNCTION__` (and `__func__` in C++) definition has changed (to make it standards compliant I believe) it has also made that macro unworkable. I've got it working using a function that builds the string by hand, but I like my macro. Am I missing an easy way to get this simple one line macro to still work under Gcc 3.3? : D

Original source