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

11,757

Solution 1

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.

Solution 2

Yes, that define is simply replaced with the passed value. This kind of define is often used if you e.g. want to determine at compile time if you want to pass a value through a translation function (#define __T(x) translate(x)) or not (#define __T(x) x).

# stringifies the passed value: http://gcc.gnu.org/onlinedocs/cpp/Stringizing.html and
## is the concatenation operator: http://gcc.gnu.org/onlinedocs/cpp/Concatenation.html

Share:
11,757
cks2k2
Author by

cks2k2

Updated on June 22, 2022

Comments

  • cks2k2
    cks2k2 over 1 year

    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?