Skip to main content
GameDev.net gamedev.net
🔒 Locked

_T( in code...

Started by Silverbolter May 14, 2004 at 8:07 PM 16 replies 1.2k views
Original Post
Silverbolter
Silverbolter
Hi, Does anyone know what _T means in Windows directx code samples? Is this a macro of some sort? Thanks!
jflanglois
jflanglois
[edit] It is, in fact, a macro.
#define _T(x) __T(x)
#define __T(x) L ## x

I think it does something related to strings.

[edited by - jflanglois on May 14, 2004 9:35:58 PM]
Chris Hare
Chris Hare
I believe it is some sort of character conversion routine, though I''ve never used Win32, so don''t quote me on that.


blah blah blah.
jflanglois
jflanglois
Hm. We learn something every day. Interestingly obscure macro though, since msdn returned "CFileFind::GetLength (Visual C++ 6.0)" when searching for _T(
Anyone care to explain "L ## x"?
dcosborn
dcosborn
## is for concatenating things with the preprocessor. Better explanation is here.

Edit: L is for declaring wide-character strings (ie: unicode strings). So L ## x, with x being a #define for "hello", results in L"hello".

[edited by - dcosborn on May 14, 2004 9:59:15 PM]
Silverbolter
Silverbolter
Thanks! That was a quick response, I spent 15 minutes searching and gave up hehe.

Its quite obsqure, and undecriptive, that isn''t good...
Muhammad Haggag
Muhammad Haggag
quote:
Original post by Silverbolter
Thanks! That was a quick response, I spent 15 minutes searching and gave up hehe.

Its quite obsqure, and undecriptive, that isn't good...

Don't forget the header file <tchar.h> for some more insights. Under unicode builds (UNICODE globally defined) those _T( "boo" ) strings turn into L"boo" strings which are stored in wchar_t's instead of char's (a wchar_t is a C++ 16-bit character type)

Muhammad Haggag,
Optimize
[edit]Angle brackets

[edited by - Coder on May 14, 2004 11:16:14 PM]

jflanglois
jflanglois
quote:
Original post by Coder
quote:
Original post by Silverbolter
Thanks! That was a quick response, I spent 15 minutes searching and gave up hehe.

Its quite obsqure, and undecriptive, that isn't good...

Don't forget the header file <tchar.h> for some more insights. Under unicode builds (UNICODE globally defined) those _T( "boo" ) strings turn into L"boo" strings which are stored in wchar_t's instead of char's (a wchar_t is a C++ 16-bit character type)

Muhammad Haggag,
Optimize
[edit]Angle brackets

[edited by - Coder on May 14, 2004 11:16:14 PM]


I suppose it would be a wchar_t * (like char *)? In which case, does std::string have unicode support?

[edit] After a bit of my own research, is it safe for me to say that std::string is unicode by default, and converts to "standard" ASCII strings when needed? Am I getting this all wrong?

Thanks to both dcosborn and AP for clearing up L ## x, by the way.

[edited by - jflanglois on May 15, 2004 1:02:56 AM]
SnprBoB86
SnprBoB86
You could just write:

L"some unicode string"

or:

"some non-unicode string"

but:

_T("some generic string")

Allows you to change the string type at compile time by changing the definition of _T via the UNICODE preprocessor definition and therefore allows you to write unicode-aware code that does not depend on unicode :-)
ArmitageIII87
ArmitageIII87
The answer to your question does std::string support UNICODE and the answer is ..... YES you just have to say wstring instead! If you don''t have the F1 key handy in Visual Studio here is some of the help on it:

typedef basic_string wstring;
Example
// string_wstring.cpp
// compile with: /EHsc
#include <string>
#include <iostream>

int main( )
{
using namespace std;
// Equivalent ways to declare an object of type
// basic_string
const basic_string s1 ( L"abc" );
wstring s2 ( L"abc" ); // Uses the typedef for wstring

// Comparison between two objects of type basic_string
if ( s1 == s2 )
cout << "The strings s1 & s2 are equal." << endl;
else
cout << "The strings s1 & s2 are not equal." << endl;
}
Output
The strings s1 & s2 are equal


Notice the lack of the _T( MYSTRING ) macro. You don''t need it but IMO I think it beats looking at L''s all over the place. Plus the macro is handy for converting back and forth from UNICODE to char. If you have Visual Studio .NET you can use wchar_t as a built in type. Just set it in your compiler project options. Here is an excerpt on wchar_t:
the type wchar_t becomes a native type that maps to __wchar_t in the same way that short maps to __int16
Muhammad Haggag
Muhammad Haggag
Armitage: You need to fix the angle brackets after basic_string.

quote:
After a bit of my own research, is it safe for me to say that std::string is unicode by default, and converts to "standard" ASCII strings when needed? Am I getting this all wrong?

typedef std::basic_string<TCHAR> tstring;
tstring boo( _T( "Some text" ) );

When UNICODE is defined, "Some text" will be UNICODE-ified. Otherwise, it'll be ANSI-fied

Muhammad Haggag,
Optimize
[edit]Sheesh! Forgot about the angle brackets myself.

[edited by - Coder on May 15, 2004 1:53:13 AM]

jflanglois
jflanglois
OK, thanks for the replies. I don''t have MSVC help installed on MSVC.NET (it''s MSDNAA)

[Edited by - jflanglois on July 16, 2004 6:42:16 PM]
Arild Fines
Arild Fines
The MSDN documentation stuff is available through MSDNAA.
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
jflanglois
jflanglois
Yeah, for MSVC 6, I haven''t found it for .NET (or is it in another location?)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.