There are preprocessor macros which define the name of a function inside it, __func__ for g++ and __FUNCTION__ for cl. (func is the c++ standard way of doing this (from dcl.fct.def.general) but it is not implmented everywhere)
For source which will be compiled by both of these compilers what should I do?
I have tried
#ifdef __func__
#define FUNCTION_NAME __func__
#else
#ifdef __FUNCTION__
#define FUNCTION_NAME __FUNCTION__
#else
#error "Function name macro not found"
#endif
#endif
but as it is not in a function the macros are not defined.
I have also tried
void implementation_detail() {
#ifdef __func__
#define FUNCTION_NAME __func__
#else
#ifdef __FUNCTION__
#define FUNCTION_NAME __FUNCTION__
#else
#error "Function name macro not found"
#endif
#endif
}
but that also gives an error.
How can I do this so it will work in multiple compliers?
#elseifdefis not a standard preprocessor directive. If you're looking for something cross-compiler, don't use a compiler-specific extension here. You'll either have to do a nested#else-#ifdef, or factor things differently.__func__macro is defined in the C++ standard and as such it is a real shame that it doesn't seem to work with all compilers.