Author Topic: STL strings and Unicode  (Read 2989 times)

sethjackson

  • Guest
STL strings and Unicode
« on: June 25, 2006, 08:07:47 pm »
If I am understanding correctly std::string is ANSI only, however STL comes to the rescue with std::wstring which is for Unicode only. Am I correct?

So is the code below correct? Then instead of using std::string, or std::wstring I could use String? I think I'm right, but I'm not sure......

Code: C++
  1. #ifndef STRING_H
  2. #define STRING_H
  3.  
  4.     #ifdef UNICODE
  5.  
  6.         #include <wstring>
  7.  
  8.         typedef std::wstring String;
  9.  
  10.     #else
  11.  
  12.         #include <string>
  13.  
  14.         typedef std::string String;
  15.  
  16.     #endif
  17.  
  18. #endif // STRING_H
  19.  

Offline TDragon

  • Lives here!
  • ****
  • Posts: 943
    • TDM-GCC
Re: STL strings and Unicode
« Reply #1 on: June 25, 2006, 10:24:34 pm »
wstring is not a standard C++ header; as far as I know, std::wstring is defined in the <string> header. Other than that, I expect your code would work.

It may interest you to know that std::string and std::wstring are only typedefs of an underlying "basic_string" template:
Code: [Select]
typedef basic_string<char, char_traits<char>, allocator<char> > string;
typedef basic_string<wchar_t, char_traits<wchar_t>, allocator<wchar_t> > wstring;

This would in essence allow you to make a string out of any data type (PODs are easiest).
https://jmeubank.github.io/tdm-gcc/ - TDM-GCC compiler suite for Windows (GCC 9.2.0 2020-03-08, 32/64-bit, no extra DLLs)

sethjackson

  • Guest
Re: STL strings and Unicode
« Reply #2 on: June 26, 2006, 01:21:18 am »
wstring is not a standard C++ header; as far as I know, std::wstring is defined in the <string> header. Other than that, I expect your code would work.

It may interest you to know that std::string and std::wstring are only typedefs of an underlying "basic_string" template:
Code: [Select]
typedef basic_string<char, char_traits<char>, allocator<char> > string;
typedef basic_string<wchar_t, char_traits<wchar_t>, allocator<wchar_t> > wstring;

This would in essence allow you to make a string out of any data type (PODs are easiest).

Cool.  8) Thanks for the answer. :)