C preprocessor Literal Construction

后端 未结 1 963
耶瑟儿~
耶瑟儿~ 2020-12-07 01:24

My problem is as follows:

I have a string literal that is macro-ed like so

#define TITLE \"Title\"

But there are instances when I n

相关标签:
1条回答
  • 2020-12-07 01:56

    Try this:

    #define WIDEN_(exp)   L##exp
    #define WIDEN(exp)    WIDEN_(exp)
    #define TITLE         "Title"
    #define W_TITLE       WIDEN(TITLE)
    

    You need to force an expansion through an intermediate macro to get what you're looking for.

    #include <stdio.h>
    
    #define WIDEN_(exp)   L##exp
    #define WIDEN(exp)    WIDEN_(exp)
    #define TITLE         "Title"
    #define W_TITLE       WIDEN(TITLE)
    
    int main(int argc, char *argv[])
    {
        printf("%s\n", TITLE);
        wprintf(L"%ls\n", W_TITLE);
        return 0;
    }
    

    Result:

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