How do I concatenate wide string literals with PRId32, PRIu64, etc.?

后端 未结 2 709
迷失自我
迷失自我 2021-02-14 10:10

Suppose I need to print a formatted string with an int32_t using the printf format specifier from .

int32_t i = 0;
pr         


        
2条回答
  •  攒了一身酷
    2021-02-14 10:40

    I know zip-all about VS C++, but you shouldn't actually need the L prefix for the concatenand of a wide string literal.

     L"%" PRId32 "\n"
    

    should work (and it does with gcc).

    From the C11 draft, §6.4.5/5: (as far as I can tell, this was roughly the same in C99, except that C99 didn't have utf-8 literals.)

    In translation phase 6, the multibyte character sequences specified by any sequence of adjacent character and identically-prefixed string literal tokens are concatenated into a single multibyte character sequence. If any of the tokens has an encoding prefix, the resulting multibyte character sequence is treated as having the same prefix; otherwise, it is treated as a character string literal. Whether differently-prefixed wide string literal tokens can be concatenated and, if so, the treatment of the resulting multibyte character sequence are implementation-defined.

    Also see §7.8.1/7, which provides the example:

    uintmax_t i = UINTMAX_MAX; // this type always exists
    wprintf(L"The largest integer value is %020"
        PRIxMAX "\n", i);
    

    A similar clause appears in the C++11 standard, §2.14.5/13. (However, in C++03, combining narrow and wide string literals was not allowed.)

    In translation phase 6, adjacent string literals are concatenated. If both string literals have the same encoding-prefix, the resulting concatenated string literal has that encoding-prefix. If one string literal has no encoding-prefix, it is treated as a string literal of the same encoding-prefix as the other operand…

    Apparently, Visual Studio does not allow this form of literal concatenation.

提交回复
热议问题