What's the difference between std::string
and std::basic_string
? And why are both needed?
views:
389answers:
3
+16
A:
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
. Yes they are both needed (or at least required by the standard).
Charles Bailey
2009-11-02 15:54:54
+7
A:
std::string is an instantiation of std::basic_string: typedef std::basic_string<char> string
. std::basic_string is necessary to have an similar interface for all type of strings (wstring for example).
pmr
2009-11-02 15:57:06
The proper term is "specialization". `string` is a *specialization* of `basic_string`.
AndreyT
2009-11-02 16:25:41
I guess you could argue about that. "specialization" is usually used to indicate inheritance. A templated {class|function} is instantiated by substituting the template arguments. There is partial template specialization but this isn't used here.
pmr
2009-11-02 17:14:06
Referring to a derived class as a 'specialization' is a less formal use of 'specialization'. The word 'specialization' isn't used like this at all in the standard (I'm fairly sure), but its used extensively to describe template specialization. The name of a template specialization can be used in contexts that don't force and implicit instantiation so specialization and instantiation are separate concepts. As well as a template specialization (the name of a template with a particular set of parameters), there are also partial specializations (class templates only) and explicit specializations.
Charles Bailey
2009-11-02 17:45:00
Using "specialization" here would be misleading. A template specialization has a specific meaning (unrelated to inheritance), but that's NOT what's used here. std::string is nothing more or less than a typedef, like: `namespace std { typedef basic_string<char> string; }` Technically, it's not really an instantiation either, but it's at least closer (instantiation, by definition, creates an instance, and a typedef only creates a name, not an instance).
Jerry Coffin
2009-11-02 17:46:03
@Jerry Coffin: `std::string` is a `typedef` _for_ a specialization of a template. How is the use of "specialization" misleading?
Charles Bailey
2009-11-02 17:53:52
@Charles: it's misleading because (with templates), "specialization" normally refers to either partial specialization, or explicit specialization, *not* a typedef for what will end up as a simple instantiation.
Jerry Coffin
2009-11-02 18:01:00
@Jerry Coffin: I agree that specialization doesn't refer to a typedef, but specialization is used to apply to any template specialization, explicitly specialized or not. This is a very common and correct usage of specialization. I don't see how the usage of "specialization" to refer to `basic_string<char>` is somehow unusual or misleading.
Charles Bailey
2009-11-02 18:07:23
@Charles, this isn't a specialization though is it? string is a typedef for an instanatiation of the basic_string template with the char type. Whether basic_string is specialized for char is a completely orthogonal matter. On VS2008 basic_string is not specialized for char.
jon hanson
2009-11-03 12:21:57
Isn't it only specialization if you provide "special" code for the template when that type is used? This is just a typedef that is short hand for instantiating the template with a class of char.
Zanson
2009-11-05 20:07:12
+4
A:
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>
.
Zanson
2009-11-02 16:09:11