Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
541 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 8VC>findstr /spin /c:"#define _T(" *.h 
crtsrcchar.h:2439:#define _T(x)       __T(x) 
includechar.h:2390:#define _T(x)       __T(x)

C:...Visual Studio 8VC>findstr /spin /c:"#define _TEXT(" *.h 
crtsrcchar.h:2440:#define _TEXT(x)    __T(x) 
includechar.h:2391:#define _TEXT(x)    __T(x)

And for completeness:

C:...Visual Studio 8VC>findstr /spin /c:"#define __T(" *.h 
crtsrcchar.h:210:#define __T(x)     L ## x 
crtsrcchar.h:889:#define __T(x)      x 
includechar.h:210:#define __T(x)     L ## x 
includechar.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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...