z32a7ul z32a7ul - 9 months ago 42
C++ Question

Should I use TCHAR today

I am starting to work on a completely new project for Windows Desktops written in C++. When I learned Windows programming, I read that using TCHAR is a great improvement because I can build an ANSI or a Unicode version of my program without changing the code. However, I never really used the option to build the ANSI version. Moreover, in the standard library of C++, there is no TCHAR, I have to create typedefs for std::string, std::stringstream, etc. and their wide string counterparts. So currently I am thinking of abandoning TCHAR in favor of wchar_t, and I collected the following advantages and disadvantages.


  • TCHAR is a macro, so if I don't use it, the front-end compiler and Intellisense will give better results.

  • It is more explicit what the type of a variable is.

  • L"" is easier to type than _T("").


  • Loss of modularity regarding the character type (even though I don't really need the ANSI version, I find using an abstract character type to be a neat feature, and what if in the future I will need a UTF-8 or UTF-32 version?).

  • I would have to postfix some API functions with W, like GetWindowTextW.

And my questions:

  • Is there an easier way in the C++ standard library to use TCHAR than the one I described above? Like a standard header file that has these typedefs?

  • Do you think that my reasoning is correct?

  • Do I miss any important point?

  • What is the state-of-the-art solution today? Do professional Windows programmers still use TCHAR (in new code)?

  • If I remove TCHAR, than should I write L"" or u"" instead of _T("")?

Answer Source

In modern windows all ANSI functions are internally converting char* to wchar_t* and calling unicode versions of same function. Basically, by adopting TCHAR instead of wchar_t you gain nothing, but have to deal with quirky syntax.