Macro without space

I have a macro that I use for debugging.

#define diagnostic_arg(message,...) fprintf(stderr,message,__VA_ARGS__) 

I found that I need to use wide characters in my program, so I would like to change only my macro and everything that works:

 #define diagnostic_arg(message,...) fwprintf(stderr,message,__VA_ARGS__) 

However, I need wide lines of characters that are defined by placing L before the beginning of the line with the quote:

 #define diagnostic_arg(message,...) fprintf(stderr,Lmessage,__VA_ARGS__) 

Now, obviously, this line is not working. But if I use L message , this will not work either. So, how to write Lmessage and do it what I would like?

+4
source share
2 answers

You can use the operator for end torrent ## :

 #define diagnostic_arg(message,...) fprintf(stderr,L##message,__VA_ARGS__) 

However, it would be better to use the TEXT macro (if you're in Visual Studio), which will do what UNICODE defines:

 #define diagnostic_arg(message,...) fprintf(stderr,TEXT(message),__VA_ARGS__) 

If you do not, TEXT can be defined as follows:

 #ifdef UNICODE #define TEXT(str) L##str #else #define TEXT(str) str #endif 

However , if you plan to use another #define as the first argument for this macro (and indeed, even if you do not plan it), you will need another layer of indirection in the macro, so the definition will be evaluated instead of pasting it with L as text. See Mooing Duck's answer for how to do this, this is actually the right way to do this, but I am not deleting this answer because I want to keep my 80 rep.

+14
source

I vaguely recall that the answer was something like

 //glues two symbols together that can't be together #define glue2(x,y) x##y #define glue(x,y) glue2(x,y) //widens a string literal #define widen(x) glue(L,x) #define diagnostic_arg(message,...) fprintf(stderr,widen(message),__VA_ARGS__) 

Glue sometimes has to be two macros (as I showed), for bizzare reasons I don’t quite understand, explained in C ++ faq

+7
source

Source: https://habr.com/ru/post/1402759/


All Articles