我应该在 C++ 上使用 _T 还是 _TEXT?字符串文字?
例如:
// 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
SDK 的一个简单的 grep 向我们展示了答案:没关系——它们是相同的。它们都变成
__T(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)
.And for completeness:
However, technically, for C++ you should be using
TEXT()
instead of_TEXT()
, but it (eventually) expands to the same thing too.承诺使用 Unicode 并仅使用
L"My String Literal"
。Commit to Unicode and just use
L"My String Literal"
.来自雷蒙德·陈:
简短版本:
_T()
是一个懒人的_TEXT()
注意:当您编写以下代码时,您需要了解源代码文本编辑器正在使用的代码页:
编译器看到的字节取决于编辑器的代码页。
From Raymond Chen:
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:
The bytes the compiler sees depends on the code page of your editor.
这里来自一位知名且受人尊敬的人的有趣读物来源。
Here's an interesting read from a well-known and respected source.
这些宏是应用程序实际上想要编译 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
我从未见过有人使用
_TEXT()
而不是_T()
。I've never seen anyone use
_TEXT()
instead of_T()
.两者都不。根据我的经验,字符串文字有两种基本类型,一种是不变的,另一种是在代码本地化时需要翻译的。
在编写代码时区分两者非常重要,这样您就不必稍后再回来找出哪个是哪个。
因此,我使用
_UT()
来表示不可翻译的字符串,使用ZZT()
(或其他易于搜索的内容)来表示需要翻译的字符串。代码中的_T()
或_TEXT()
实例是尚未正确分类的字符串文字的证据。_UT
和ZZT
均 #define 到 _TEXTNeither. 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, andZZT()
(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
andZZT
are both #defined to _TEXT两者都不要使用,也请不要使用 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.