<html>

<head>
<meta http-equiv="Content-Language" content="en-us">
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Fixing vector</title>
</head>

<body>

<p>Doc. no.&nbsp;&nbsp; WG21/N2160=J16/07-0020<br>
Date:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<!--webbot bot="Timestamp" s-type="EDITED" s-format="%Y-%m-%d" startspan -->2007-01-12<!--webbot bot="Timestamp" endspan i-checksum="12079" --><br>
Project:&nbsp;&nbsp;&nbsp;&nbsp; Programming Language C++<br>
Reply to:&nbsp;&nbsp; Beman Dawes &lt;<a href="mailto:bdawes@acm.org">bdawes@acm.org</a>&gt;</p>

<h1>Library Issue 96: Fixing <code>vector&lt;bool&gt;</code></h1>
<p>Library Issue 96, <i>Vector&lt;bool&gt; is not a containe</i>r, has been open since 1998. 
It needs to be fixed for C++0x.</p>
<p>See <a href="#References">References</a> for details.</p>
<p>In 1998, admitting that the committee made a mistake was controversial. 
Since then Java has had to deprecate such significant portions of their libraries 
that the idea C++ would be ridiculed for deprecating a single minor template 
specialization seems quaint.</p>
<p>To ensure a smooth transition, the <code>dynamic_bitset</code> library is proposed for 
C++0x (rather than TR2) and explicit guidance is provided for both library 
implementors and ordinary users.</p>
<h2>Deprecate or remove?</h2>
<p>Undesirable library features are usually removed only after 
having been first <a href="#deprecate">deprecated</a> in a prior standard. This two step process puts 
users on notice that the feature is going to later be removed, giving them 
plenty of time to migrate existing code to the replacement feature. To do 
otherwise would break existing code without prior warning.</p>
<p><code>vector&lt;bool&gt;</code> is unusual, however, because it is a template 
specialization. Other than the <code>flip()</code> member function, most 
practical functionality is the same as for the primary template. Thus removing 
the specialization will not necessarily break user code, although it may alter the 
performance characteristics.</p>
<p>I tried several of the code search engines to get an idea how commonly <code>
std::vector&lt;bool&gt;::flip</code> is used, but could not come up with reliable 
numbers.&nbsp; It was too hard to separate out use of <code>std::vector&lt;bool&gt;::flip</code> 
from uses of various non-standard bit vector functions of the same name. It did become clear, 
however, that <code>std::vector&lt;bool&gt;::flip</code> use is rare enough that examples are very difficult to find, and 
that dynamic bit 
vectors, both from Boost and elsewhere are in common use, particularly in 
graphics applications.</p>
<p>I personally think that it would be preferable to remove the <code>vector&lt;bool&gt;</code> 
specialization rather than deprecate it, since the impact on users is so 
minimal, and since the sooner <code>vector&lt;bool&gt;</code> 
goes away, the better. The proposed resolution below specifies removal. The committee can 
change that to deprecation if preferred.</p>
<h2>What we say...</h2>
<p>Regardless of the choice of deprecation or removal, a smooth transition path 
must be provided for both implementors and users. In early discussions of how to fix <code>vector&lt;bool&gt;</code>, the LWG felt 
that any solution needed to provide clear guidance regarding that transition 
path to Standard Library 
implementors, the C++ community as a whole, and to users. The remainder of this 
section provides that guidance.</p>
<h3>Guidance for implementors</h3>
<p>During a transition period, Standard Library implementors are encouraged to 
give users a choice:</p>
<blockquote>
  <pre>// supply default (remove for default of no vector&lt;bool&gt; specialization)
# if !defined( defined(STD_VECTOR_BOOL_SPECIAL) &amp;&amp; !defined(STD_VECTOR_BOOL_NOT_SPECIAL)
#   define STD_VECTOR_BOOL_SPECIAL
# endif

// validate user supplied defines
# if defined(STD_VECTOR_BOOL_SPECIAL) &amp;&amp; defined(STD_VECTOR_BOOL_NOT_SPECIAL)
#   error Both STD_VECTOR_BOOL_SPECIAL and STD_VECTOR_BOOL_NOT_SPECIAL defined - you must supply one at most
# endif

// supply specialization if called for
# if defined(STD_VECTOR_BOOL_SPECIAL)
  ... specialization here ...
# endif</pre>
</blockquote>
<p>Implementations should default to STD_VECTOR_BOOL_SPECIAL (by supplying the first four 
lines of the above code) as long as their users are best served by defaulting to 
the specialization for <code>vector&lt;bool&gt;</code>. When their users would no 
longer best be served by defaulting to the specialization for <code>vector&lt;bool&gt;</code>, 
implementations can change the default to no specialization by removing the 
first four lines of code. When implementations no longer wish to support 
specialization for <code>vector&lt;bool&gt;</code>, they can eliminate all of the 
above code, including the specialization. If implementations wish to issue an 
error once the specialization choices are removed, they can do something like:</p>
<blockquote>
  <pre>// verify users no long depend on STD_VECTOR_BOOL_SPECIAL
# if defined(STD_VECTOR_BOOL_SPECIAL)
#   error STD_VECTOR_BOOL_SPECIAL no longer supported - use std::dynamic_bitset instead
# endif</pre>
</blockquote>
<h3>Guidance for&nbsp; the C++ community</h3>
<p>The 1998 C++ Standard Library's <code>vector&lt;bool&gt;</code> specialization was 
a mistake - it does not meet the requirements for a container, its iterator does 
not meet the iterator requirements, and it supplies a sometimes unfortunate 
optimization. Search the web for &quot;vector&lt;bool&gt; problems&quot; for details.</p>
<p>The C++0x standard removes 
the 
C++ Standard Library's <code>vector&lt;bool&gt;</code> specialization. A new library,
<code>dynamic_bitset</code>, is provided for those needing efficient bit 
manipulation. Guidance is given for users to enable a 
smooth transition path.</p>
<h3>Guidance for users with existing code using the <code>vector&lt;bool&gt;</code> specialization</h3>
<ul>
  <li>If you <b>don't care</b> about <code>vector&lt;bool&gt;</code>'s special 
  features, do nothing. Code that does not expect <code>vector&lt;bool&gt;</code> 
  to have special characteristics will continue to work without change.<br>
&nbsp;</li>
  <li>If you <b>do</b> want the <code>vector&lt;bool&gt;</code> specialization, define <code>
  STD_VECTOR_BOOL_SPECIAL</code>. Plan to eventually switch to <code>
  std::dynamic_bitset</code> since it isn't good practice to depend on removed features.<br>
&nbsp;</li>
  <li>If you <b>do not</b> want the <code>vector&lt;bool&gt;</code> specialization, 
  define <code>STD_VECTOR_BOOL_NOT_SPECIAL</code>.</li>
</ul>
<h3>Guidance for users writing new code</h3>
<ul>
  <li>If you need a container with iterators and don't care about space 
  optimizations or bit twiddling functions, use <code>vector&lt;bool&gt;</code>. 
  Define <code>STD_VECTOR_BOOL_NOT_SPECIAL</code> if you want to be sure you 
  don't get the old specialization.<br>
&nbsp;</li>
  <li>If you don't need a container with iterators, care about space, or need 
  bit twiddling functions, use <code>std::dynamic_bitset</code>.</li>
</ul>
<h2>Proposed Resolution</h2>
<p>Remove 23.2.6 [vector.bool], Class 
vector&lt;bool&gt;, in its entirety.</p>
<p>Add N2050, &quot;Proposal to Add a Dynamically Sizeable Bitset to the Standard 
Library Technical Report (Revision 1)&quot; to the C++0x working paper. See
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2050.pdf">www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2050.pdf</a>.</p>

<h2><a name="References">References</a></h2>

<p>N2130, C++ Standard Library Active Issues List,
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2130.html#96">
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2130.html#96</a></p>

<p>N1185, vector&lt;bool&gt; Is Nonconforming, and Forces Optimization Choice, Herb 
Sutter, 22nd Feb. 1999. Library.
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/1999/n1185.pdf">
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/1999/n1185.pdf</a></p>

<p>N1847, vector&lt;bool&gt;: More Problems, Better Solutions, Herb Sutter, 20th 
Oct. 1999. Library.
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1847.pdf">
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1847.pdf</a></p>

<p>Effective STL: 50 Specific Ways to Improve Your Use of the Standard Template 
Library, Scott Meyers, 2001, Addison-Wesley. pp79  82.</p>

<h2>Meaning of &quot;<a name="deprecate">deprecate</a>&quot;</h2>

<p>The definition of <i>deprecate</i> is given in appendix D of the standard as 
&quot;Normative for the current edition of the Standard, but not guaranteed to be 
part of the Standard in future revisions.&quot;</p>

<hr>

</body>

</html>
