What's the difference between std::string and std::basic_string? And why are both needed?

std::basic_string is a class template for making strings out of character types, std::string is a typedef for a specialization of that class template for char.


std::string is an instantiation of std::basic_string<T>:

typedef std::basic_string<char> string

std::basic_string is necessary to have a similar interface for all type of strings (wstring for example).


A std::string is an instantiation of the std::basic_string template with a type of char. You need both so that you can make strings of things besides char, such a std::basic_string<wchar_t> for a string of wide characters. Or if you want a string with 32 bit elements, std::basic_string<unsigned int>.