Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the _T Macro effect std::string constructor?

Tags:

c++

visual-c++

I inherited some old code which needs to stay multibyte/unicode compatible. It currently uses the _T() macro for this. I was wondering if std::strings are introduced, is _T() still needed?

e.g

std::string foo(_T("hello"));

Or is it now redundant?

like image 894
Red Riding Hood Avatar asked Jun 27 '26 04:06

Red Riding Hood


1 Answers

It's not only unneeded. It's wrong. The _T macro expands to convert the string literal to a wide character (wchar_t) string literal if UNICODE is defined. However, the std::string constructor never expects a wide character string (const wchar_t*). It always expects const char*.

What you might do, if you were still concerned about this sort of thing (compiling for both Unicode and ANSI versions of the WinAPI), is define a new type based on std::basic_string

typedef std::basic_string<TCHAR> tstring;

And then this would be correct:

tstring foo(_T("hello"));
like image 187
Benjamin Lindley Avatar answered Jun 28 '26 18:06

Benjamin Lindley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!