<?xml version="1.0" encoding="UTF-8" standalone="no"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><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=" ISO C++ , library " /><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>No, you cannot write code of the form</p><pre class="programlisting">#include <bitset>void foo (size_t n){std::bitset<n> bits;....}</pre><p>because <code class="code">n</code> must be known at compile time. Yourcompiler is correct; it is not a bug. That's the way templateswork. (Yes, it <span class="emphasis"><em>is</em></span> a feature.)</p><p>There are a couple of ways to handle this kind of thing. Pleaseconsider all of them before passing judgement. They include, inno particular order:</p><div class="itemizedlist"><ul type="disc"><li><p>A very large N in <code class="code">bitset<N></code>.</p></li><li><p>A container<bool>.</p></li><li><p>Extremely weird solutions.</p></li></ul></div><p><span class="emphasis"><em>A very large N in<code class="code">bitset<N></code>.Β Β </em></span> It has beenpointed out a few times in newsgroups that N bits only takes up(N/8) bytes on most systems, and division by a factor of eight ispretty impressive when speaking of memory. Half a megabyte givenover to a bitset (recall that there is zero space overhead forhousekeeping info; it is known at compile time exactly how largethe set is) will hold over four million bits. If you're usingthose bits as status flags (e.g.,β<span class="quote">changed</span>β/β<span class="quote">unchanged</span>β flags), that's a<span class="emphasis"><em>lot</em></span> of state.</p><p>You can then keep track of the β<span class="quote">maximum bit used</span>βduring some testing runs on representative data, make note of howmany of those bits really need to be there, and then reduce N toa smaller number. Leave some extra space, of course. (If youplan to write code like the incorrect example above, where thebitset is a local variable, then you may have to talk yourcompiler into allowing that much stack space; there may be zerospace overhead, but it's all allocated inside the object.)</p><p><span class="emphasis"><em>A container<bool>.Β Β </em></span> TheCommittee made provision for the space savings possible with that(N/8) usage previously mentioned, so that you don't have to dowasteful things like <code class="code">Container<char></code> or<code class="code">Container<short int></code>. Specifically,<code class="code">vector<bool></code> is required to be specialized forthat space savings.</p><p>The problem is that <code class="code">vector<bool></code> doesn'tbehave like a normal vector anymore. There have been recentjournal articles which discuss the problems (the ones by HerbSutter in the May and July/August 1999 issues of C++ Report coverit well). Future revisions of the ISO C++ Standard will changethe requirement for <code class="code">vector<bool></code>specialization. In the meantime, <code class="code">deque<bool></code>is recommended (although its behavior is sane, you probably willnot get the space savings, but the allocation scheme is differentthan that of vector).</p><p><span class="emphasis"><em>Extremely weird solutions.Β Β </em></span> Ifyou have access to the compiler and linker at runtime, you can dosomething insane, like figuring out just how many bits you need,then writing a temporary source code file. That file contains aninstantiation of <code class="code">bitset</code> for the required number ofbits, inside some wrapper functions with unchanging signatures.Have your program then call the compiler on that file usingPosition Independent Code, then open the newly-created objectfile and load those wrapper functions. You'll have aninstantiation of <code class="code">bitset<N></code> for the exact<code class="code">N</code> that you need at the time. Don't forget to deletethe temporary files. (Yes, this <span class="emphasis"><em>can</em></span> be, and<span class="emphasis"><em>has been</em></span>, done.)</p><p>This would be the approach of either a visionary genius or araving lunatic, depending on your programming and managementstyle. Probably the latter.</p><p>Which of the above techniques you use, if any, are up to you andyour intended application. Some time/space profiling isindicated if it really matters (don't just guess). And, if youmanage to do anything along the lines of the third category, theauthor would love to hear from you...</p><p>Also note that the implementation of bitset used in libstdc++ has<a class="ulink" href="../ext/sgiexts.html#ch23" target="_top">some extensions</a>.</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></p><p>Bitmasks do not take char* nor const char* arguments in theirconstructors. This is something of an accident, but you can readabout the problem: follow the library's β<span class="quote">Links</span>β fromthe homepage, and from the C++ information β<span class="quote">defectreflector</span>β link, select the library issues list. Issuenumber 116 describes the problem.</p><p>For now you can simply make a temporary string object using theconstructor expression:</p><pre class="programlisting">std::bitset<5> b ( std::string(β<span class="quote">10110</span>β) );</pre><p>instead of</p><pre class="programlisting">std::bitset<5> b ( β<span class="quote">10110</span>β ); // invalid</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>