views:

192

answers:

2

Is there an string equivalent to LPTSTR? I know of string and wstring. Is there a tstring?

+8  A: 

You could define one:

typedef std::basic_string<TCHAR> mystring;
...
mystring test = _T("Hello World!");
AraK
Would you need to make new traits too?
GMan
AFAIK `TCHAR` is either `char` or `wchar_t`. The standard provides a specialization for both types.
AraK
GMan: tstring becomes an alias for either string or wstring (they are typedefs of basic\_string<char> and basic\_string<wchar\_t>, respectively).
Roger Pate
Indeed, I was thinking completely wrong for a bit. This solution seems rather clean, then. (Just rename it `tstring` and throw it in a namespace :P)
GMan
Hah, oops, I actually read the answer as 'tstring' instead of 'mystring', since that is the common name.
Roger Pate
+1 elegant way to do it.
Jason D
+1  A: 

Another option (doesn't require windows.h):

#if defined(_UNICODE) || defined(UNICODE)
  typedef std::wstring ustring_t;
  typedef wchar_t uchar_t;
#else
  typedef std::string ustring_t;
  typedef char uchar_t;
#endif

Usage:

ustring_t mystr = TEXT("hello world");
Kirill V. Lyadvinsky
@Downvoter: Care to comment? Which bit do you disagree with?
Kirill V. Lyadvinsky
Not the downvoter, but without `<windows.h>` you don't have the `TEXT()` macro either.
MSalters
@MSalters, Only "usage" part requires TEXT macro. That's just an example.
Kirill V. Lyadvinsky