User forums > General (but related to Code::Blocks)

STL strings and Unicode

(1/1)

sethjackson:
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: (cpp) ---#ifndef STRING_H
#define STRING_H

    #ifdef UNICODE

        #include <wstring>

        typedef std::wstring String;

    #else

        #include <string>

        typedef std::string String;

    #endif

#endif // STRING_H

--- End code ---

TDragon:
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: ---typedef basic_string<char, char_traits<char>, allocator<char> > string;
typedef basic_string<wchar_t, char_traits<wchar_t>, allocator<wchar_t> > wstring;

--- End code ---

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

sethjackson:

--- Quote from: TDragon 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: ---typedef basic_string<char, char_traits<char>, allocator<char> > string;
typedef basic_string<wchar_t, char_traits<wchar_t>, allocator<wchar_t> > wstring;

--- End code ---

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

--- End quote ---

Cool.  8) Thanks for the answer. :)

Navigation

[0] Message Index

Go to full version