explain this macro
#define __T(x) L ## x
Found in code from one of the MFC source header file. It is mostly used for converting strings to ........ (I don't know what). If I am correct it converts strings to LPCTSTR...don't know what that type is either...
I can't seem to convert char* into LPCTSTR. While MFC file handling, the following code will always return e开发者_运维问答rror while trying to open the file...
char* filepath = "C:\\Program Files\\Microsoft Office\\Office12\\BITMAPS\\STYLES\\GLOBE.WMF";
if( !file.Open((LPCTSTR)filepath , CFile::modeRead, &fexp) )
{
fexp.ReportError();
return 1;
}
But instead if I wrote it this way, it doesn't give error:
if( !file.Open( _T("C:\\Program Files\\Microsoft Office\\Office12\\BITMAPS\\STYLES\\GLOBE.WMF") , CFile::modeRead, &fexp) )
{
fexp.ReportError();
return 1;
}
I am looking at passing a variable as the first argument to the CFile::Open() method.
The ## operator is a preprocessor concatenation operator. That is, this is valid code:
#define DECLARE_PTR(X) typedef std::auto_ptr<X> X##Ptr
DECLARE_PTR(int); // gets expanded to typedef std::auto_ptr<int> intPtr
intPtr i(new int(1));
In your case, the _T
macro prepends the Long conversion symbol (L
) to the input given. This only works with string literals. That means you can't write
char* str = "ABC";
wchar_t* wstr = _T(str); // error: Lstr is undefined
but you can safely write
char* str = "ABC";
LPTSTR wstr = _T("ABC"); // OK, gets expanded to wchar_t * wstr = L"ABC";
// when UNICODE is defined
// and char * wstr = "ABC"; when unicode is not defined
The L
operator is a convertor of char and char* literals to a Long representation (from byte
-wide representation to sizeof(wchar_t)
-wide representation).
The macro is simply stringizing L with the argument so that:
_T("xyz")
becomes:
L"xyz"
This is the way to make a wstring
but, in the non-Unicode versions, _T
will map to nothing, so you'll get regular strings there.
_T()
allows you to set up your string literals so that you can build as either Unicode or non-unicode.
In non-unicode builds it evaluates to nothing so a string literal is represented as "XYZ"
which is a normal narrow string. In a unicode build it evaluates to L
(L"XYZ"
) which tells the compiler that the string literal is a wide character string. This and the various "T" string typedefs LPCTSTR
etc. Allow you to write code that builds correctly for unicode and non-unicode builds.
Note that google is your friend, simply typing _T
into google gives several useful results...
精彩评论