2

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?

4
  • #elseifdef is 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. Commented Nov 27, 2013 at 14:20
  • The __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. Commented Nov 27, 2013 at 14:21
  • @Agentlien I had a look in my copy, but I made the mistake of looking under 16 Preprocessing directives. I'll update with this info. Commented Nov 27, 2013 at 15:45
  • @JoeZ Thanks, I didn't realise that was non-standard I'll change it Commented Nov 27, 2013 at 15:46

1 Answer 1

3

I have found that there is a boost macro for this, BOOST_CURRENT_FUNCTION.

From looking at the source code here how this is implemented is they use an inline function like so.

inline void current_function_helper()
{

#if defined(__GNUC__) || (defined(__MWERKS__) && (__MWERKS__ >= 0x3000)) || (defined(__ICC) && (__ICC >= 600)) || defined(__ghs__) || defined(__DMC__)`
...

(also it is declared in a namespace called detail which is very wise)

I will use this.

EDIT: implementing __func__ will be in the Visual Studio customer technology preview out now, see here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.