3

If writing code which may be compiled with different character types, is TCHAR still the right type to use e.g std::basic_stringstream<TCHAR>

Or is there now some official C++/STL type which is preferred, in the way wchar_t replaces WCHAR, true replaces TRUE and nullptr replaces NULL?

I'm talking about cases where I'm using classes like basic_string, templated explicitly with TCHAR.

Mr. Boy
  • 60,845
  • 93
  • 320
  • 589
  • 1
    No. I consider wchar_t as nonsense (hence TCHAR, which is either char or wchar_t, is nonsense, too). Use utf8 if possible –  Nov 27 '15 at 17:05
  • Could you clarify your use case? Do you know in advance if what you want to stream is null-terminated? Variable-width? Fixed-width with embedded nulls? – rubicks Nov 27 '15 at 17:12
  • @rubicks I'm not an expert on this stuff, does my edit clarify a bit? – Mr. Boy Nov 27 '15 at 17:21
  • No (It makes it worse) –  Nov 27 '15 at 17:24
  • 2
    `If writing code which may be compiled with different character types` Why? There is pretty mucn no reason anymore to write pre-unicode-compatible programs on Windows/Linux/Mac. – deviantfan Nov 27 '15 at 17:24
  • @deviantfan well you answered it yourself. I'm not writing a program. I'm maintaining one! – Mr. Boy Nov 27 '15 at 17:27
  • 1
    @Mr.Boy treat TCHAR as deprecated at best and harmful at worst. Also, consider delegating your unicode manipulations to libicu (http://site.icu-project.org/) or similar. – rubicks Nov 27 '15 at 17:50
  • @rubicks thanks for the link, I'm not familiar with that. – Mr. Boy Nov 27 '15 at 17:55
  • See http://stackoverflow.com/questions/17103925/how-well-is-unicode-supported-in-c11 – MSalters Nov 28 '15 at 01:24

1 Answers1

8

No. TCHAR allows you to switch between char and wchar_t based on Microsoft's _UNICODE macro and using Microsoft's various other 'T' macros. None of this is relevant to the standard C++ APIs.

If you're maintaining a legacy C++ program that uses TCHAR then you can just continue to use TCHAR.

For writing new programs what I've seen people prefer has been to avoid using legacy codepages entirely and instead use char as UTF-8, and then on Windows to use the *W APIs unconditionally, converting between UTF-8 char strings and wchar_t at the API boundary.

bames53
  • 86,085
  • 15
  • 179
  • 244
  • What does **"then on Windows to use the *W APIs unconditionally, converting between UTF-8 char strings and wchar_t at the API boundary."** mean? – Second Person Shooter Dec 25 '21 at 01:37
  • 1
    @UnrealEngine5ComingSoon It means use the APIs ending in W directly, without the macros that switch between the W and A versions based on the UNICODE preprocessor definition. So instead of using the macro SetWindowText you use the function SetWindowTextW, and the text you want to set would be originally a `char` string using the UTF-8 encoding, which you convert to `wchar_t` immediately before you need to pass that text to the function. Windows has since begun supporting UTF-8 as the active code page, so you could instead ensure that's set, use the A functions, and directly pass UTF-8 data. – bames53 Jan 05 '22 at 18:00
  • Thank you very much. – Second Person Shooter Jan 06 '22 at 05:33