我应该在 C++ 字符串文字上使用 _T 还是 _TEXT?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2074579/
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 22:00:50  来源:igfitidea点击:

Should I use _T or _TEXT on C++ string literals?

c++winapibackwards-compatibilityliterals

提问by Joe

For example:

例如:

// This will become either SomeMethodA or SomeMethodW,
// depending on whether _UNICODE is defined.
SomeMethod( _T( "My String Literal" ) );

// Becomes either AnotherMethodA or AnotherMethodW.
AnotherMethod( _TEXT( "My Text" ) );

I've seen both. _T seems to be for brevity and _TEXT for clarity. Is this merely a subjective programmer preference or is it more technical than that? For instance, if I use one over the other, will my code not compile against a particular system or some older version of a header file?

我两个都见过。_T 似乎是为了简洁,_TEXT 是为了清楚起见。这仅仅是程序员的主观偏好还是比这更具技术性?例如,如果我使用一个而不是另一个,我的代码是否不会针对特定系统或某个旧版本的头文件进行编译?

采纳答案by i_am_jorf

A simple grep of the SDK shows us that the answer is that it doesn't matter—they are the same. They both turn into __T(x).

SDK 的一个简单的 grep 向我们展示了答案是没关系——它们是相同的。他们都变成__T(x).

C:\...\Visual Studio 8\VC>findstr /spin /c:"#define _T(" *.h 
crt\src\tchar.h:2439:#define _T(x)       __T(x) 
include\tchar.h:2390:#define _T(x)       __T(x)

C:\...\Visual Studio 8\VC>findstr /spin /c:"#define _TEXT(" *.h 
crt\src\tchar.h:2440:#define _TEXT(x)    __T(x) 
include\tchar.h:2391:#define _TEXT(x)    __T(x)

And for completeness:

为了完整性:

C:\...\Visual Studio 8\VC>findstr /spin /c:"#define __T(" *.h 
crt\src\tchar.h:210:#define __T(x)     L ## x 
crt\src\tchar.h:889:#define __T(x)      x 
include\tchar.h:210:#define __T(x)     L ## x 
include\tchar.h:858:#define __T(x)      x

However, technically, for C++ you should be using TEXT()instead of _TEXT(), but it (eventually) expands to the same thing too.

但是,从技术上讲,对于 C++,您应该使用TEXT()代替_TEXT(),但它(最终)也扩展为相同的东西。

回答by i_am_jorf

Commit to Unicode and just use L"My String Literal".

承诺使用 Unicode,只需使用L"My String Literal".

回答by Ian Boyd

From Raymond Chen:

来自Raymond Chen

TEXT vs. _TEXT vs. _T, and UNICODE vs. _UNICODE

The plain versions without the underscore affect the character set the Windows header files treat as default. So if you define UNICODE, then GetWindowText will map to GetWindowTextW instead of GetWindowTextA, for example. Similarly, the TEXT macro will map to L"..." instead of "...".

The versions with the underscore affect the character set the C runtime header files treat as default. So if you define _UNICODE, then _tcslen will map to wcslen instead of strlen, for example. Similarly, the _TEXT macro will map to L"..." instead of "...".

What about _T? Okay, I don't know about that one. Maybe it was just to save somebody some typing.

TEXT 与 _TEXT 与 _T,以及 UNICODE 与 _UNICODE

没有下划线的普通版本会影响 Windows 头文件视为默认的字符集。因此,例如,如果您定义 UNICODE,则 GetWindowText 将映射到 GetWindowTextW 而不是 GetWindowTextA。同样,TEXT 宏将映射到 L"..." 而不是 "..."。

带有下划线的版本会影响 C 运行时头文件视为默认的字符集。因此,例如,如果您定义了 _UNICODE,那么 _tcslen 将映射到 wcslen 而不是 strlen。同样,_TEXT 宏将映射到 L"..." 而不是 "..."。

_T呢?好吧,我不知道那个。也许只是为了节省某人的打字时间。

Short version: _T()is a lazy man's _TEXT()

短版:_T()是懒人的_TEXT()

Note: You need to be aware of what code-page your source code text editor is using when you write:

注意:在编写以下代码时,您需要了解源代码文本编辑器使用的代码页:

_TEXT("Some string containing ?ontaining");
TEXT("xtended characters.");

The bytes the compiler sees depends on the code page of your editor.

编译器看到的字节取决于编辑器的代码页。

回答by dirkgently

Here's an interesting read from a well-known and respected source.

是来自知名且受人尊敬的来源的有趣读物。

Similarly, the _TEXT macro will map to L"..." instead of "...".

What about _T? Okay, I don't know about that one. Maybe it was just to save somebody some typing.

同样,_TEXT 宏将映射到 L"..." 而不是 "..."。

_T呢?好吧,我不知道那个。也许只是为了节省某人的打字时间。

回答by egrunin

I've never seen anyone use _TEXT()instead of _T().

我从未见过有人使用_TEXT()代替_T().

回答by John Knoeller

Neither. In my experience there are two basic types of string literals, those that are invariant, and those that need to be translated when your code is localized.

两者都不。根据我的经验,有两种基本类型的字符串文字,一种是不变的,另一种是在您的代码本地化时需要翻译的。

It's important to distinguish between the two as you write the code so you don't have to come back and figure out which is which later.

在编写代码时将两者区分开来很重要,这样您就不必稍后再回来弄清楚哪个是哪个。

So I use _UT()for untranslatable strings, and ZZT()(or something else that is easy to search on) for strings that will need to be translated. Instances of _T()or _TEXT()in the code are evidence of string literals that have not yet be correctly categorized.

因此,我将_UT()用于不可翻译的字符串,以及ZZT()(或其他易于搜索的内容)用于需要翻译的字符串。代码中_T()或 的实例是_TEXT()尚未正确分类的字符串文字的证据。

_UTand ZZTare both #defined to _TEXT

_UT并且ZZT都被 #defined 定义为 _TEXT

回答by Terry Mahaffey

These macros are a hold over from the days when an application might have actually wanted to compile both a unicode and ANSI version.

这些宏是从应用程序可能实际上想要编译 unicode 和 ANSI 版本的日子里保留下来的。

There is no reason to do this today - this is all vestigial. Microsoft is stuck with supporting every possible configuration forever, but you aren't. If you are not compiling to both ANSI and Unicode (and no one is, let's be honest) just go to with L"text".

今天没有理由这样做——这都是残留的。微软坚持永远支持所有可能的配置,但你不是。如果您没有同时编译为 ANSI 和 Unicode(老实说,没有人编译),请使用 L"text"。

And yes, in case it wasn't clear by now: _T == _TEXT

是的,如果现在还不清楚:_T == _TEXT

回答by Pavel Radzivilovsky

Use neither, and also please don't use the L"..." crap. Use UTF-8 for all strings, and convert them just before passing to microsoft APIs.

两者都不使用,也请不要使用 L"..." 废话。对所有字符串使用 UTF-8,并在传递给微软 API 之前将它们转换。