Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Macro string: what does #define __T(x) x mean? And __T(#x)?

Tags:

c++

c

visual-c++

What does this mean?

#define __T(x)  x

Just return x?

I am seeing code using syntax I've not seen before:

#define CREATE_ENCODER(CODEC) \
strcpy(codecName, __T(#CODEC); \
pCodec = new CODEC##VideoEncoder();
if(parFileName) \
{  pEncoderParams = new CODEC##EncoderParams; \
}

What is the # for?

like image 386
cks2k2 Avatar asked Nov 29 '22 02:11

cks2k2


1 Answers

Actually __T is used to turn string to wchar_t* or char*, in tchar.h you have:

#define __T(x)      L ## x

used when UNICODE is enabled and

#define __T(x)      x

when it is disabled

If your code is to be compiled on both UNICODE and non-UNICODE compilations you use:

TCHAR* sz = __T("My text");

most WINAPI functions use TCHAR* or some of its form

Actually I prefer _T() version, never knew __T version exists, at the bottom of tchar.h you have it defined:

#define _T(x)       __T(x)
#define _TEXT(x)    __T(x)

So back to your example:

strcpy(codecName, __T(#CODEC)); \

is equivalent to:

strcpy(codecName, "CODEC"); \

on NON-unicode build, and

strcpy(codecName, L"CODEC"); \

on UNICODE build

VERY IMPORTANT!!: using _T or __T is not really enough to make sure you code is UNICODE compilant. It will make it easier to call WINAPI functions. You must prepare your code to work with UNICODE.

like image 191
marcinj Avatar answered Dec 05 '22 07:12

marcinj