4

I was looking at some windows directx application code and saw that they use the macro _T(x) to set their window name, when looking at the macro definiton I saw this

#define _T(x)       __T(x)

then I followed and looked at __T

#define __T(x)      x

Why does this exist?

Mat
  • 202,337
  • 40
  • 393
  • 406
Noah
  • 143
  • 1
  • 5
  • May have something to do with string conversions. Can you find an example where it's used? Edit: https://stackoverflow.com/questions/240353/convert-a-preprocessor-token-to-a-string is what I was thinking of, but it's a different case. – Thomas Mar 31 '20 at 18:10
  • [Unicode Programming Summary](https://learn.microsoft.com/en-us/cpp/text/unicode-programming-summary?view=vs-2019) `_T` is a macro that, depending on settings, may do something. – crashmstr Mar 31 '20 at 18:14
  • Used here: WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, WndProc, 0L, 0L, GetModuleHandle(NULL), NULL, NULL, NULL, NULL, _T("ImGui Example"), NULL }; or here: HWND hwnd = ::CreateWindow(wc.lpszClassName, _T("Dear ImGui DirectX9 Example"), WS_OVERLAPPEDWINDOW, 100, 100, 1280, 800, NULL, NULL, wc.hInstance, NULL); – Noah Mar 31 '20 at 18:16
  • I guess DirectX once worked with MBCS and Unicode apps. Thus `_T` was the macro used to denote a wide or narrow string depending on the build type. Also, note that `GetModuleHandle` is also a macro -- it is actually either `GetModuleHandleA` or `GetModuleHandleW`, again depending on the build. So it is in the same situation as `_T`, i.e. changing depending on the build type. – PaulMcKenzie Mar 31 '20 at 18:16
  • I know how it works with GetModuleHandle. When looking at the macro defintion of GetModuleHandle, it shows the two cases directly, while it didnt when looking at _T – Noah Mar 31 '20 at 18:19

1 Answers1

8

_T(x) is usually a macro for a platform agnostic text character type. In some platforms it may resolve to just x. In others using wide characters it may resolve to L ## x, e.g. _T("abc") may resolve to either "abc", or L"abc".

Rotem
  • 21,452
  • 6
  • 62
  • 109
  • 2
    Don't know why this got downvoted; it's the correct answer. You might also mention that it's a windowsism. – Pete Becker Mar 31 '20 at 18:16
  • Oh okay that makes sense, regardless where I look its always the same definition though, guess I just didnt look deep enough, thanks! – Noah Mar 31 '20 at 18:17
  • @Noah It's possible that if you change some settings in your build environment, like support for unicode, you may say it defined differently. – Rotem Mar 31 '20 at 18:19