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

发布于 2024-08-17 20:23:51 字数 377 浏览 3 评论 0原文

例如:

// 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" ) );

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

屋檐 2024-08-24 20:23:51

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)

为了完整起见:

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

但是,技术上,对于 C++,您应该使用 TEXT() 而不是 _TEXT(),但它(最终)会扩展也有同样的事情。

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).

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.

帅冕 2024-08-24 20:23:51

承诺使用 Unicode 并仅使用 L"My String Literal"

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

木有鱼丸 2024-08-24 20:23:51

来自雷蒙德·陈

TEXT 与 _TEXT 与 _T,以及 UNICODE 与 _UNICODE

没有的普通版本
下划线影响字符集
Windows 头文件视为
默认。所以如果你定义 UNICODE,
然后 GetWindowText 将映射到
GetWindowTextW 代替
例如,获取窗口文本A。
同样,TEXT 宏将映射到
L“...”而不是“...”。

带下划线的版本
影响 C 运行时的字符集
头文件视为默认。所以如果
您定义 _UNICODE,然后 _tcslen 将
映射到 wcslen 而不是 strlen,对于
例子。类似地,_TEXT 宏
将映射到 L"..." 而不是 "..."。

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

简短版本:_T() 是一个懒人的 _TEXT()

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

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

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

From 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.

Short version: _T() is a lazy man's _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.

猫弦 2024-08-24 20:23:51

这里来自一位知名且受人尊敬的人的有趣读物来源。

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

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

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.

等待圉鍢 2024-08-24 20:23:51

这些宏是应用程序实际上想要编译 unicode 和 ANSI 版本的时代遗留下来的。

今天没有理由这样做——这都是残余的。 Microsoft 始终坚持支持所有可能的配置,但您却不然。如果您没有同时编译为 ANSI 和 Unicode(老实说,没有人这样做),只需使用 L"text"。

是的,以防现在还不清楚:_T == _TEXT

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

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".

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

蓦然回首 2024-08-24 20:23:51

我从未见过有人使用 _TEXT() 而不是 _T()

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

乖不如嘢 2024-08-24 20:23:51

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

在编写代码时区分两者非常重要,这样您就不必稍后再回来找出哪个是哪个。

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

_UTZZT 均 #define 到 _TEXT

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 and ZZT are both #defined to _TEXT

咽泪装欢 2024-08-24 20:23:51

两者都不要使用,也请不要使用 L"..." 废话。
对所有字符串使用 UTF-8,并在传递给 Microsoft API 之前对其进行转换。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文