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

后端 未结 3 881
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-03 19:23

I am working on a project where I have many constant strings formed by concatenation (numbers, etc.).

For example, I have a LOCATION macro that formats

相关标签:
3条回答
  • 2020-12-03 19:29

    Just using an empty wide string literal should work:

     #define WLOCATION L"" __FILE__ "(" STR(__LINE__) ")"
    
    0 讨论(0)
  • 2020-12-03 19:32

    To concatenate two wide literal strings you could use

    L"wide_a" L"wide_b"
    

    So you could define

    #define WLOCATION WIDEN(__FILE__) L"(" WIDEN(STR(__LINE__)) L")"
    

    (Note: not tested on MSVC++)

    0 讨论(0)
  • 2020-12-03 19:33

    According to the C standard (aka "ISO-9899:1999" aka "C99"), Visual C is wrong and gcc is correct. That standard states, section 6.4.5/4:

    In translation phase 6, the multibyte character sequences specified by any sequence of adjacent character and wide string literal tokens are concatenated into a single multibyte character sequence. If any of the tokens are wide string literal tokens, the resulting multibyte character sequence is treated as a wide string literal; otherwise, it is treated as a character string literal.

    So you could file a complaint. Arguably, the previous version of the C standard (aka "C89" aka "C90" aka "ANSI C") did not mandate merging of wide strings with non-wide strings. Although C99 is now more than ten years old, it seems that Microsoft has no interest in making its C compiler conforming. Some users have reported being able to access some "C99" features by compiling C code as if it was C++ code, because C++ includes these features -- and for C++, Microsoft made an effort. But this does not seem to extend to the preprocessor.

    In the C89 dialect, I think that what you are looking for is not possible (actually I am pretty sure of it, and since I have written my own preprocessor I think I know what I am talking about). But you could add an extra parameter and propagate 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) t(")")
    #define LOCATION      LOCATION_(N)
    #define WLOCATION     LOCATION_(W)
    

    which should work on both gcc and Visual C (at least, it works for me, using Visual C 2005).

    Side note: you should not define macros with a name beginning with an underscore. These names are reserved, so by using them you could clash with some names used in system headers or in future versions of the compiler. Instead of _WIDEN, use WIDEN_.

    0 讨论(0)
提交回复
热议问题