OSDN Git Service

fcdba6d2358ac395fcf6682bc218afa345abed71
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / doc / html / manual / bitset.html
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>bitset</title><meta name="generator" content="DocBook XSL Stylesheets V1.74.0" /><meta name="keywords" content="&#10;      ISO C++&#10;    , &#10;      library&#10;    " /><link rel="home" href="../spine.html" title="The GNU C++ Library Documentation" /><link rel="up" href="associative.html" title="Chapter 17. Associative" /><link rel="prev" href="associative.html" title="Chapter 17. Associative" /><link rel="next" href="containers_and_c.html" title="Chapter 18. Interacting with C" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">bitset</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="associative.html">Prev</a> </td><th width="60%" align="center">Chapter 17. Associative</th><td width="20%" align="right"> <a accesskey="n" href="containers_and_c.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="containers.associative.bitset"></a>bitset</h2></div></div></div><div class="sect2" lang="en" xml:lang="en"><div class="titlepage"><div><div><h3 class="title"><a id="associative.bitset.size_variable"></a>Size Variable</h3></div></div></div><p>
4         No, you cannot write code of the form
5       </p><pre class="programlisting">
6       #include &lt;bitset&gt;
7
8       void foo (size_t n)
9       {
10           std::bitset&lt;n&gt;   bits;
11           ....
12       } 
13    </pre><p>
14      because <code class="code">n</code> must be known at compile time.  Your
15      compiler is correct; it is not a bug.  That's the way templates
16      work.  (Yes, it <span class="emphasis"><em>is</em></span> a feature.)
17    </p><p>
18      There are a couple of ways to handle this kind of thing.  Please
19      consider all of them before passing judgement.  They include, in
20      no particular order:
21    </p><div class="itemizedlist"><ul type="disc"><li><p>A very large N in <code class="code">bitset&lt;N&gt;</code>.</p></li><li><p>A container&lt;bool&gt;.</p></li><li><p>Extremely weird solutions.</p></li></ul></div><p>
22      <span class="emphasis"><em>A very large N in
23      <code class="code">bitset&lt;N&gt;</code>.  </em></span> It has been
24      pointed out a few times in newsgroups that N bits only takes up
25      (N/8) bytes on most systems, and division by a factor of eight is
26      pretty impressive when speaking of memory.  Half a megabyte given
27      over to a bitset (recall that there is zero space overhead for
28      housekeeping info; it is known at compile time exactly how large
29      the set is) will hold over four million bits.  If you're using
30      those bits as status flags (e.g.,
31      “<span class="quote">changed</span>”/“<span class="quote">unchanged</span>” flags), that's a
32      <span class="emphasis"><em>lot</em></span> of state.
33    </p><p>
34      You can then keep track of the “<span class="quote">maximum bit used</span>”
35      during some testing runs on representative data, make note of how
36      many of those bits really need to be there, and then reduce N to
37      a smaller number.  Leave some extra space, of course.  (If you
38      plan to write code like the incorrect example above, where the
39      bitset is a local variable, then you may have to talk your
40      compiler into allowing that much stack space; there may be zero
41      space overhead, but it's all allocated inside the object.)
42    </p><p>
43      <span class="emphasis"><em>A container&lt;bool&gt;.  </em></span> The
44      Committee made provision for the space savings possible with that
45      (N/8) usage previously mentioned, so that you don't have to do
46      wasteful things like <code class="code">Container&lt;char&gt;</code> or
47      <code class="code">Container&lt;short int&gt;</code>.  Specifically,
48      <code class="code">vector&lt;bool&gt;</code> is required to be specialized for
49      that space savings.
50    </p><p>
51      The problem is that <code class="code">vector&lt;bool&gt;</code> doesn't
52      behave like a normal vector anymore.  There have been recent
53      journal articles which discuss the problems (the ones by Herb
54      Sutter in the May and July/August 1999 issues of C++ Report cover
55      it well).  Future revisions of the ISO C++ Standard will change
56      the requirement for <code class="code">vector&lt;bool&gt;</code>
57      specialization.  In the meantime, <code class="code">deque&lt;bool&gt;</code>
58      is recommended (although its behavior is sane, you probably will
59      not get the space savings, but the allocation scheme is different
60      than that of vector).
61    </p><p>
62      <span class="emphasis"><em>Extremely weird solutions.  </em></span> If
63      you have access to the compiler and linker at runtime, you can do
64      something insane, like figuring out just how many bits you need,
65      then writing a temporary source code file.  That file contains an
66      instantiation of <code class="code">bitset</code> for the required number of
67      bits, inside some wrapper functions with unchanging signatures.
68      Have your program then call the compiler on that file using
69      Position Independent Code, then open the newly-created object
70      file and load those wrapper functions.  You'll have an
71      instantiation of <code class="code">bitset&lt;N&gt;</code> for the exact
72      <code class="code">N</code> that you need at the time.  Don't forget to delete
73      the temporary files.  (Yes, this <span class="emphasis"><em>can</em></span> be, and
74      <span class="emphasis"><em>has been</em></span>, done.)
75    </p><p>
76      This would be the approach of either a visionary genius or a
77      raving lunatic, depending on your programming and management
78      style.  Probably the latter.
79    </p><p>
80      Which of the above techniques you use, if any, are up to you and
81      your intended application.  Some time/space profiling is
82      indicated if it really matters (don't just guess).  And, if you
83      manage to do anything along the lines of the third category, the
84      author would love to hear from you...
85    </p><p>
86      Also note that the implementation of bitset used in libstdc++ has
87      <a class="ulink" href="../ext/sgiexts.html#ch23" target="_top">some extensions</a>.
88    </p></div><div class="sect2" lang="en" xml:lang="en"><div class="titlepage"><div><div><h3 class="title"><a id="associative.bitset.type_string"></a>Type String</h3></div></div></div><p>
89       </p><p>
90      Bitmasks do not take char* nor const char* arguments in their
91      constructors.  This is something of an accident, but you can read
92      about the problem: follow the library's “<span class="quote">Links</span>” from
93      the homepage, and from the C++ information “<span class="quote">defect
94      reflector</span>” link, select the library issues list.  Issue
95      number 116 describes the problem.
96    </p><p>
97      For now you can simply make a temporary string object using the
98      constructor expression:
99    </p><pre class="programlisting">
100       std::bitset&lt;5&gt; b ( std::string(“<span class="quote">10110</span>”) );
101    </pre><p>
102      instead of
103    </p><pre class="programlisting">
104       std::bitset&lt;5&gt; b ( “<span class="quote">10110</span>” );    // invalid
105     </pre></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="associative.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="associative.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="containers_and_c.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 17. Associative </td><td width="20%" align="center"><a accesskey="h" href="../spine.html">Home</a></td><td width="40%" align="right" valign="top"> Chapter 18. Interacting with C</td></tr></table></div></body></html>