在 VC++ 中将字符串文字提升为 wchar_t 的标准宏 [英] Standard macro to promote string literal to wchar_t in VC++

查看:24
本文介绍了在 VC++ 中将字符串文字提升为 wchar_t 的标准宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有标准宏可以使用 Visual Studio 2005 将用预处理器宏替换的字符字符串文字转换为 wchar_t 字符串?

Is there a standard macro to convert a char string literal that is substituted with a preprocessor macro to a wchar_t string using Visual Studio 2005?

例如这有效:

wchar_t* test = L"Hello World";

但这不会:

#define HELLOWORLD "Hello World"
wchar_t* test = L(HELLOWORLD);

我正在与不同平台上的几个不同项目共享一个包含许多国际化字符串的头文件,所以我不想改变头本身,也不想添加 _T() 因为这是平台相关的.所以我希望 unicode 转换在我的源代码中,而不是在标题中.我知道我可以编写自己的替换宏,如此处所示,但是想知道VS中是否有标准方法?

I am sharing a header file containing many internationalised strings with several different projects on different platforms, so I don't want to alter the header itself, nor add _T() since this is platform dependent. So I want the unicode conversion to be in my source, not in the header. I know I can write my own substitution macro as shown here, but am wondering if there is a standard method in VS?

对于编写国际化代码的任何人来说,这似乎是一个非常常见的场景,但我似乎找不到 VS2005 提供的预定义宏.

This would seem a very common scenario for anyone writing internationalised code, but I can't seem to find a predefined macro delivered with VS2005.

推荐答案

不,没有针对此的标准"宏.标准是用 L 作为宽字符串的前缀,并从窄字符串中省略前缀.

No, there is no "standard" macro for this. The standard is to prefix wide strings with an L and omit the prefix from narrow strings.

为什么需要存在这样的宏的唯一原因是当您针对不支持 Unicode 的各种平台时.在这种情况下,一切都需要是一个窄字符串.如果您不处理缺乏 Unicode 支持的平台,那么 everything 应该一直是一个宽字符串.

The only reason why such a macro even needs to exist is when you're variously targeting platforms that don't support Unicode. In that case, everything needs to be a narrow string. If you're not dealing with platforms that lack Unicode support, then everything should probably be a wide string all the time.

_TTEXT 宏在 Windows 头文件中提供正是为了这个目的:维护一个可以为 Windows NT 编译的单一代码库,它支持 Unicode,以及缺乏 Unicode 支持的 Windows 9x.

The _T and TEXT macros are provided in the Windows headers for precisely this purpose: maintaining a single code base that can be compiled both for Windows NT, which supports Unicode, and Windows 9x, which lacks Unicode support.

如果您还没有包含 Windows 标头,我无法想象为什么您需要这样的宏,但是如果您这样做了,那么自己编写一个非常简单.除了你需要知道什么时候字符串文字应该是宽字符串,什么时候它们应该是窄字符串.对此也没有标准"#define.Windows 标头使用 UNICODE 预处理器符号,但您不能依赖它在其他平台上定义.所以现在您又回到了起点.

I can't imagine why you would need such a macro if you aren't already including the Windows headers, but if you do, it's pretty simple to write one yourself. Except than you're going to need to know when string literals should be wide strings and when they should be narrow strings. There's no "standard" #define for this, either. The Windows headers use the UNICODE pre-processor symbol, but you can't rely on this being defined on other platforms. So now you're back to where you started from.

为什么你认为你需要一个宏呢?如果您将字符串的类型硬编码为 wchar_t*,那么您总是会想要使用宽字符文字,所以您总是 想使用 L 前缀.

Why do you think you need a macro for this, again? If you're hardcoding the type of the string as wchar_t*, then you're always going to want to use a wide character literal, so you always want to use the L prefix.

当您使用 Windows 标头中的 _T 和/或 TEXT 宏时,您也不会将字符串的类型硬编码为 wchar_t*.相反,您使用的是 TCHAR 宏,它会根据 UNICODE 符号的定义自动解析为适当的字符类型.

When you're using the _T and/or TEXT macros from the Windows headers, you're also not hard-coding the type of the string as wchar_t*. Instead, you're using the TCHAR macro, which automatically resolves to the appropriate character type depending on the definition of the UNICODE symbol.

这篇关于在 VC++ 中将字符串文字提升为 wchar_t 的标准宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆