[ACCEPTED]-Should I use _T or _TEXT on C++ string literals?-literals

Accepted answer
Score: 24

A simple grep of the SDK shows us that the 4 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 3 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 2 should be using TEXT() instead of _TEXT(), but it (eventually) expands 1 to the same thing too.

Score: 21

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


Score: 17

From Raymond Chen:

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

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

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

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

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

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

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

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

Score: 8

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

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

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

Score: 5

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


Score: 5

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

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

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

Score: 4

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

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

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

_UT and ZZT are both #defined to 1 _TEXT

More Related questions