1 <?xml version=
"1.0" encoding=
"UTF-8" standalone=
"no"?>
2 <!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3 <html xmlns=
"http://www.w3.org/1999/xhtml"><head><meta http-equiv=
"Content-Type" content=
"text/html; charset=UTF-8" /><title>Arbitrary Character Types
</title><meta name=
"generator" content=
"DocBook XSL Stylesheets V1.73.2" /><meta name=
"keywords" content=
" ISO C++ , library " /><link rel=
"start" href=
"../spine.html" title=
"The GNU C++ Library Documentation" /><link rel=
"up" href=
"bk01pt05ch13.html" title=
"Chapter 13. String Classes" /><link rel=
"prev" href=
"bk01pt05ch13s02.html" title=
"Case Sensitivity" /><link rel=
"next" href=
"bk01pt05ch13s04.html" title=
"Tokenizing" /></head><body><div class=
"navheader"><table width=
"100%" summary=
"Navigation header"><tr><th colspan=
"3" align=
"center">Arbitrary Character Types
</th></tr><tr><td width=
"20%" align=
"left"><a accesskey=
"p" href=
"bk01pt05ch13s02.html">Prev
</a> </td><th width=
"60%" align=
"center">Chapter
13. String Classes
</th><td width=
"20%" align=
"right"> <a accesskey=
"n" href=
"bk01pt05ch13s04.html">Next
</a></td></tr></table><hr /></div><div class=
"sect1" lang=
"en" xml:
lang=
"en"><div class=
"titlepage"><div><div><h2 class=
"title" style=
"clear: both"><a id=
"strings.string.character_types"></a>Arbitrary Character Types
</h2></div></div></div><p>
4 </p><p>The
<code class=
"code">std::basic_string
</code> is tantalizingly general, in that
5 it is parameterized on the type of the characters which it holds.
6 In theory, you could whip up a Unicode character class and instantiate
7 <code class=
"code">std::basic_string
<my_unicode_char
></code>, or assuming
8 that integers are wider than characters on your platform, maybe just
9 declare variables of type
<code class=
"code">std::basic_string
<int
></code>.
10 </p><p>That's the theory. Remember however that basic_string has additional
11 type parameters, which take default arguments based on the character
12 type (called
<code class=
"code">CharT
</code> here):
13 </p><pre class=
"programlisting">
14 template
<typename CharT,
15 typename Traits = char_traits
<CharT
>,
16 typename Alloc = allocator
<CharT
> >
17 class basic_string { .... };
</pre><p>Now,
<code class=
"code">allocator
<CharT
></code> will probably Do The Right
18 Thing by default, unless you need to implement your own allocator
20 </p><p>But
<code class=
"code">char_traits
</code> takes more work. The char_traits
21 template is
<span class=
"emphasis"><em>declared
</em></span> but not
<span class=
"emphasis"><em>defined
</em></span>.
22 That means there is only
23 </p><pre class=
"programlisting">
24 template
<typename CharT
>
27 static void foo (type1 x, type2 y);
29 };
</pre><p>and functions such as char_traits
<CharT
>::foo() are not
30 actually defined anywhere for the general case. The C++ standard
31 permits this, because writing such a definition to fit all possible
32 CharT's cannot be done.
33 </p><p>The C++ standard also requires that char_traits be specialized for
34 instantiations of
<code class=
"code">char
</code> and
<code class=
"code">wchar_t
</code>, and it
35 is these template specializations that permit entities like
36 <code class=
"code">basic_string
<char,char_traits
<char
>></code> to work.
37 </p><p>If you want to use character types other than char and wchar_t,
38 such as
<code class=
"code">unsigned char
</code> and
<code class=
"code">int
</code>, you will
39 need suitable specializations for them. For a time, in earlier
40 versions of GCC, there was a mostly-correct implementation that
41 let programmers be lazy but it broke under many situations, so it
42 was removed. GCC
3.4 introduced a new implementation that mostly
43 works and can be specialized even for
<code class=
"code">int
</code> and other
45 </p><p>If you want to use your own special character class, then you have
46 <a class=
"ulink" href=
"http://gcc.gnu.org/ml/libstdc++/2002-08/msg00163.html" target=
"_top">a lot
47 of work to do
</a>, especially if you with to use i18n features
48 (facets require traits information but don't have a traits argument).
49 </p><p>Another example of how to specialize char_traits was given
<a class=
"ulink" href=
"http://gcc.gnu.org/ml/libstdc++/2002-08/msg00260.html" target=
"_top">on the
50 mailing list
</a> and at a later date was put into the file
<code class=
"code">
51 include/ext/pod_char_traits.h
</code>. We agree
52 that the way it's used with basic_string (scroll down to main())
53 doesn't look nice, but that's because
<a class=
"ulink" href=
"http://gcc.gnu.org/ml/libstdc++/2002-08/msg00236.html" target=
"_top">the
54 nice-looking first attempt
</a> turned out to
<a class=
"ulink" href=
"http://gcc.gnu.org/ml/libstdc++/2002-08/msg00242.html" target=
"_top">not
55 be conforming C++
</a>, due to the rule that CharT must be a POD.
56 (See how tricky this is?)
57 </p></div><div class=
"navfooter"><hr /><table width=
"100%" summary=
"Navigation footer"><tr><td width=
"40%" align=
"left"><a accesskey=
"p" href=
"bk01pt05ch13s02.html">Prev
</a> </td><td width=
"20%" align=
"center"><a accesskey=
"u" href=
"bk01pt05ch13.html">Up
</a></td><td width=
"40%" align=
"right"> <a accesskey=
"n" href=
"bk01pt05ch13s04.html">Next
</a></td></tr><tr><td width=
"40%" align=
"left" valign=
"top">Case Sensitivity
</td><td width=
"20%" align=
"center"><a accesskey=
"h" href=
"../spine.html">Home
</a></td><td width=
"40%" align=
"right" valign=
"top"> Tokenizing
</td></tr></table></div></body></html>