如何在 C++ 中使用宽字符串文字而不将 L 放在每个字符串的前面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/260125/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 14:07:22  来源:igfitidea点击:

How to use wide string literals in c++ without putting L in front of each one

c++stringliterals

提问by Fry

You'll have to forgive my ignorance, but I'm not used to using wide character sets in c++, but is there a way that I can use wide string literals in c++ without putting an L in front of each literal?

你必须原谅我的无知,但我不习惯在 c++ 中使用宽字符集,但是有没有一种方法可以在 c++ 中使用宽字符串文字而不在每个文字前面放一个 L?

If so, how?

如果是这样,如何?

回答by Ferruccio

No, there isn't. You have to use the L prefix (or a macro such as _T() with VC++ that expands to L anyway when compiled for Unicode).

不,没有。您必须使用 L 前缀(或诸如 _T() 之类的宏,在为 Unicode 编译时无论如何都会扩展为 L 的 VC++ )。

回答by shoosh

The new C++0x Standard defines another way of doing this:
http://en.wikipedia.org/wiki/C%2B%2B0x#New_string_literals

新的 C++0x 标准定义了另一种方法:http:
//en.wikipedia.org/wiki/C%2B%2B0x#New_string_literals

回答by ShoeLace

on a related note.. i'm trying to do the following

在相关说明上..我正在尝试执行以下操作

#define  get_switch( m )   myclass::getSwitch(L##m)

which is a macro the will expand

这是一个将扩展的宏

get_switch(isrunning)

into

进入

myclass::getswitch(L"isrunning")

this works fine in c++ visualstudio 2008

这在 c++ visualstudio 2008 中运行良好

but when i compile the same code under mac Xcode (for iphone) i get the error:

但是当我在 mac Xcode(对于 iphone)下编译相同的代码时,我收到错误:

error: 'L' was not defined in this scope.

EDIT: Solution

编辑:解决方案

#define  get_switch( m )   myclass::getSwitch(L ## #m)

this works on both vc++ and mac xcode (gcc)

这适用于 vc++ 和 mac xcode (gcc)

回答by Adam Rosenfield

Why do you not want to prefix string literals with an L? It's quite simple - strings without an L are ANSI strings (const char*), strings with an L are wide-character strings (const wchar_t*). There is the TEXT()macro, which makes a string literal into an ANSI or a wide-character string depending on of the current project is set to use Uncode:

为什么你不想用 L 前缀字符串文字?这很简单 - 没有 L 的字符串是 ANSI 字符串 ( const char*),带有 L 的字符串是宽字符字符串 ( const wchar_t*)。有一个TEXT()宏,它根据当前项目设置为使用 Uncode 将字符串文字转换为 ANSI 或宽字符串:


#ifdef UNICODE
#define TEXT(s) L ## s
#else
#define TEXT(s) s
#endif

There's also the _T()macro, which is equivalent to TEXT().

还有一个_T()宏,相当于TEXT().