c++ - How to convert concatenated strings to wide-char with the C preprocessor? -


I am working on a project where I have several fixed strings, which are created by the insertion (number, etc.) Have been.

For example, I have a LOCATION macro in the formatting __ file __ and __ line __ in a string Which I can use to know where I am the code, when message or error printing:

  #define _STR (x) # x #define STR (x) _STR (x) ), It will format a place like "file.cpp (42)" #define LOCATION __FILE__ "(" STR (__ LINE__) ")"  

The problem occurs when I try to convert the result to a wide-string:

  #define _WIDEN (x) L ##x #define WIDEN (x) _WIDEN (x) # Define WLOCATION WIDEN (LOCATION)  

This works fine with GCC, and the results "L" file.cpp (42) "is being inserted into my code." However, when MSWC + + (Using Visual C ++ 2008 Express), I get an error:

  Error: narrow "file.cpp" (" < / Pre> 

I understand that L subsystem I only tried to add the first word in my expression. I also tried:

  #define _WIDEN (which "works", but string  L "\" file. Cpp \ "\" (\ "\" 42 \ "\" \ "\" " which is clearly not very convenient (and which I am not seeing), especially this macro is comparable to other macros In simple terms. 

So, my question is: how can I implement it on MSWC ++ in whole expression, then I am getting a GCC Mr. I get results? I will not make another string with all the wide tokens, because I have to maintain two macros for each one, which is not very convenient and can reach the bug. Plus, I also need a narrow version of each string, so using all-wide strings is not an opt. C standard (aka "ISO-9899: 1999" alias ") Unfortunately

In translation stage 6, the adjacent character and wide strings are added to a single multibyte character sequence specified by a sequence of literal tokens, in a sequence of multi-bit character sequence is . If any of the tokens have lexical tokens of a wide string, the resulting multibyte character sequence is considered as a comprehensive string; Otherwise, it is considered as a letter string.

Therefore you can file a complaint, of course, in the previous version of C standard (aka "C89" aka "ANSI C") to merge wider wires with non-wired stars Did not have the mandate Although the C 99 is more than ten years old now, it seems that there is no interest in compiling the Microsoft Cc compiler. Some users have compiled C code and have been able to access some "C 99" features as it was a C ++ code, because these features are included in C ++ - and for C ++, Microsoft Has made an attempt but it does not spread to the preprocessor.

In the C89 dialect, I think that whatever you want is not possible (in fact I am quite convinced of it, and since I wrote my preprocessor I think that I Know what I'm talking about). But you can add an extra parameter and can broadcast it:

  #define w (x) w_ (x) #define w_ (x) L ## x #define N (x ) X #define STR (x, t) STR_ (x, t) #define STR_ (x, t) t (#x) #define LOCATION_ (t) t (__ FILE__) T ("(" ") STR (__ LINE__, t) (")" # Position defined location LOCATION_ (N) # Definition WLOCATION LOCATION_ (W)  

Should work on both GCC and Visual C (at least, this Works for me, uses Visual C 2005).

Side note: With the name starting with an underscore, These names are reserved, so using them you can fight with some names used in future versions of system titles or compilers, instead of _WIDEN , < Code> WIDEN_ .


Comments

Popular posts from this blog

sql - dynamically varied number of conditions in the 'where' statement using LINQ -

asp.net mvc - Dynamically Generated Ajax.BeginForm -

Debug on symbian -