如何将宽字符串文字与PRId32,PRIu64等连接? [英] How do I concatenate wide string literals with PRId32, PRIu64, etc.?
问题描述
假设我需要使用<inttypes.h>
中的printf格式说明符使用int32_t
打印格式化的字符串.
Suppose I need to print a formatted string with an int32_t
using the printf format specifier from <inttypes.h>
.
int32_t i = 0;
printf("%" PRId32 "\n", i);
如果我尝试在Visual C ++ 2013中对宽字符执行相同的操作,则会失败:
If I try to do the same with wide characters in Visual C++ 2013, it fails:
#define W_(val) L ## val
#define W(val) W_(val)
wprintf(L"%" W(PRId32) L"\n", i);
错误C2308:串联不匹配的字符串
error C2308: concatenating mismatched strings
如何将宽字符串文字与格式转换说明符宏连接起来?
How do I concatenate wide string literals with format conversion specifier macros?
推荐答案
问题是双重的.首先,正如rici的回答所述,C99和C ++ 11都添加了对将窄字符串文字和宽字符串文字连接在一起的支持,因此您不必通过在L前面加宽窄文字的范围.VisualC ++尚不支持此功能. C或C ++的功能.
The problem is twofold. First, as noted in rici's answer, C99 and C++11 both added support for concatenating narrow string literals and wide string literals together, so you should not need to widen the narrow literal by prepending the L. Visual C++ does not yet support this feature for either C or C++.
由于编译器尚不支持此功能,因此我们应该在库中使您可以使用答案中的一种类似技术来显式扩展这些字符串文字.不幸的是,我们以一种可以将它们扩展为多个字符串文字的方式定义了这些宏.例如,PRId32
扩展为"l" "d"
.
Because the compiler does not yet support this feature, we should in the libraries make it possible for you to explicitly widen these string literals using a technique like the one in your answer. Unfortunately, we've defined these macros in a way such that they may expand to multiple string literals. E.g., PRId32
expands to "l" "d"
.
这是有效的,但是它确实阻止了您扩展,因为没有办法将L
放在第二个字符串文字之前(使"d"
成为L"d"
).恐怕如果不自行(重新)定义宏,就无法找到一种方法来完成这项工作.
This is valid, but it does prevent you from widening, because there is no way to prepend an L
to the second string literal (to make "d"
into L"d"
). I'm afraid I don't see a way to make this work without (re)defining the macros yourself.
我在内部打开了一个错误,以便如果编译器在下一发行版的预处理期间不添加对混合宽度文字串联的支持,我们可以重新访问这些定义,以便可能显式地扩展它们.
I've opened a bug internally so that if the compiler does not add support for concatenation of mixed-width literals during preprocessing in the next release, we can revisit these definitions to possibly make it possible to widen them explicitly.
这篇关于如何将宽字符串文字与PRId32,PRIu64等连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!