0

The accepted answer to the question C++ concatenating __FILE__ and __LINE__ macros? work for FILE and LINE, but I would like to add the func macro too.

#define S1(x) #x
#define S2(x) S1(x)
#define S3(x) x

#define LOCATION __FILE__ " : " S3(__func__) " : " S2(__LINE__)

this gives error

log.cpp|15 col 15 error| note: in definition of macro ‘S3’

Also tried,

#define LOCATION __FILE__ " : " __func__ " : " S2(__LINE__)

gives error

log.cpp|30 col 9 error| note: in expansion of macro ‘LOCATION’

I understand that LINE was a integer hence needed #x, but func is a char array. How do I get this working? Any help?

2
  • __func__ is not a macro. Commented Jun 4, 2016 at 6:29
  • How do you plan to use LOCATION? Commented Jun 4, 2016 at 6:35

2 Answers 2

2

As cpplearner mentioned correctly __func__ is not a macro, it's a special function-local predefined variable. I achieved desired result by

#define S1(x) #x
#define S2(x) S1(x)
#define LOCATION string(__func__) + " : " S2(__LINE__) + " : "

and I use it by sending the string to a wrapper log function

void log(string s) {
     cout << s <<endl;
}

void abc(){
    log(LOCATION + string("some message "));
}

Outputs:

abc : 23 : some message 
Sign up to request clarification or add additional context in comments.

Comments

0
__func__ is a variable (not a macro unlike __FILE__ or __LINE__)

This related question Treating __func__ as a string literal instead of a predefined identifier has good explanation.

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.