1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 2<html> 3<head> 4<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 5<title>Rationale</title> 6<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css"> 7<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 8<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset"> 9<link rel="up" href="../stl_interfaces.html" title="Chapter 38. Boost.STLInterfaces"> 10<link rel="prev" href="../boost/stl_interfaces/v2/view_interface.html" title="Type definition view_interface"> 11<link rel="next" href="../thread.html" title="Chapter 39. Thread 4.8.0"> 12</head> 13<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 14<table cellpadding="2" width="100%"><tr> 15<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td> 16<td align="center"><a href="../../../index.html">Home</a></td> 17<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td> 18<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> 19<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> 20<td align="center"><a href="../../../more/index.htm">More</a></td> 21</tr></table> 22<hr> 23<div class="spirit-nav"> 24<a accesskey="p" href="../boost/stl_interfaces/v2/view_interface.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../stl_interfaces.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../thread.html"><img src="../../../doc/src/images/next.png" alt="Next"></a> 25</div> 26<div class="section"> 27<div class="titlepage"><div><div><h2 class="title" style="clear: both"> 28<a name="boost_stlinterfaces.rationale"></a><a class="link" href="rationale.html" title="Rationale">Rationale</a> 29</h2></div></div></div> 30<h4> 31<a name="boost_stlinterfaces.rationale.h0"></a> 32 <span class="phrase"><a name="boost_stlinterfaces.rationale.there_are_minimal_derived_type_contraints"></a></span><a class="link" href="rationale.html#boost_stlinterfaces.rationale.there_are_minimal_derived_type_contraints">There 33 Are Minimal Derived-Type Contraints</a> 34 </h4> 35<p> 36 This is the constraint on the <code class="computeroutput"><span class="identifier">Derived</span></code> 37 template parameter to <code class="computeroutput"><a class="link" href="../boost/stl_interfaces/v1/iterator_interface.html" title="Struct template iterator_interface">iterator_interface</a></code>, <code class="computeroutput"><a class="link" href="../boost/stl_interfaces/v1/view_interface.html" title="Struct template view_interface">view_interface</a></code> 38 and <code class="computeroutput"><a class="link" href="../boost/stl_interfaces/v1/sequ_1_3_39_11_2_5_1_1_1_1.html" title="Struct template sequence_container_interface">sequence_container_interface</a></code>: 39 </p> 40<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">enable_if_t</span><span class="special"><</span> 41 <span class="identifier">std</span><span class="special">::</span><span class="identifier">is_class</span><span class="special"><</span><span class="identifier">Derived</span><span class="special">>::</span><span class="identifier">value</span> <span class="special">&&</span> 42 <span class="identifier">std</span><span class="special">::</span><span class="identifier">is_same</span><span class="special"><</span><span class="identifier">Derived</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">remove_cv_t</span><span class="special"><</span><span class="identifier">Derived</span><span class="special">>>::</span><span class="identifier">value</span><span class="special">></span> 43</pre> 44<p> 45 This prevents instantiating an interface template with an <code class="computeroutput"><span class="keyword">int</span></code>, 46 a <code class="computeroutput"><span class="keyword">const</span></code> type, a reference type, 47 etc. 48 </p> 49<p> 50 Further constraints are not possible (for instance, that <code class="computeroutput"><a class="link" href="../boost/stl_interfaces/v1/view_interface.html" title="Struct template view_interface">view_interface</a></code> is given a <code class="computeroutput"><span class="identifier">Derived</span></code> template parameter for a type that 51 has a <code class="computeroutput"><span class="identifier">begin</span><span class="special">()</span></code> 52 and <code class="computeroutput"><span class="identifier">end</span><span class="special">()</span></code>), 53 because <code class="computeroutput"><span class="identifier">Derived</span></code> is an incomplete 54 type within each *<code class="computeroutput"><span class="identifier">_interface</span></code> 55 template. 56 </p> 57<h4> 58<a name="boost_stlinterfaces.rationale.h1"></a> 59 <span class="phrase"><a name="boost_stlinterfaces.rationale.using_a_special_access_granting__code__phrase_role__keyword__struct__phrase___code_"></a></span><a class="link" href="rationale.html#boost_stlinterfaces.rationale.using_a_special_access_granting__code__phrase_role__keyword__struct__phrase___code_">Using 60 a Special Access-Granting <code class="computeroutput"><span class="keyword">struct</span></code></a> 61 </h4> 62<p> 63 The interface templates rely mostly on public members provided by their <code class="computeroutput"><span class="identifier">Derived</span></code> template parameter. However, <code class="computeroutput"><a class="link" href="../boost/stl_interfaces/v1/iterator_interface.html" title="Struct template iterator_interface">iterator_interface</a></code> 64 requires you to supply <code class="computeroutput"><span class="identifier">base_reference</span><span class="special">()</span></code> functions if you want it to act like an adaptor. 65 Since at least the non-<code class="computeroutput"><span class="keyword">const</span></code> overload 66 provides a non-<code class="computeroutput"><span class="keyword">const</span></code> lvalue reference 67 to one of your types data members, it will break the encapsulation of many 68 types to leave <code class="computeroutput"><span class="identifier">base_reference</span><span class="special">()</span></code> a public member. To allow users to keep these 69 overloads private, <code class="computeroutput"><a class="link" href="../boost/stl_interfaces/access.html" title="Struct access">access</a></code> exists. 70 </p> 71<h4> 72<a name="boost_stlinterfaces.rationale.h2"></a> 73 <span class="phrase"><a name="boost_stlinterfaces.rationale._classname_alt__boost__stl_interfaces__v1__iterator_interface___code__phrase_role__identifier__iterator_interface__phrase___code___classname__can_act_like_an_adaptor__and_the_other_interface_templates_can_t"></a></span><a class="link" href="rationale.html#boost_stlinterfaces.rationale._classname_alt__boost__stl_interfaces__v1__iterator_interface___code__phrase_role__identifier__iterator_interface__phrase___code___classname__can_act_like_an_adaptor__and_the_other_interface_templates_can_t">iterator_interface 74 Can Act Like an Adaptor, And the Other Interface Templates Can't</a> 75 </h4> 76<p> 77 There wouldn't be much point in adding this functionality to <code class="computeroutput"><a class="link" href="../boost/stl_interfaces/v1/view_interface.html" title="Struct template view_interface">view_interface</a></code>, because it only 78 uses the <code class="computeroutput"><span class="identifier">begin</span><span class="special">()</span></code> 79 and <code class="computeroutput"><span class="identifier">end</span><span class="special">()</span></code> 80 of the <code class="computeroutput"><span class="identifier">Derived</span></code> type anyway. 81 </p> 82<p> 83 For <code class="computeroutput"><a class="link" href="../boost/stl_interfaces/v1/sequ_1_3_39_11_2_5_1_1_1_1.html" title="Struct template sequence_container_interface">sequence_container_interface</a></code> 84 it also does not make much sense. Consider how many container adaptors you've 85 written. That's a use case that does not come up often. 86 </p> 87<h4> 88<a name="boost_stlinterfaces.rationale.h3"></a> 89 <span class="phrase"><a name="boost_stlinterfaces.rationale._classname_alt__boost__stl_interfaces__v1__iterator_interface___code__phrase_role__identifier__iterator_interface__phrase___code___classname__takes_a_lot_of_template_parameters__and_the_other_interface_templates_don_t"></a></span><a class="link" href="rationale.html#boost_stlinterfaces.rationale._classname_alt__boost__stl_interfaces__v1__iterator_interface___code__phrase_role__identifier__iterator_interface__phrase___code___classname__takes_a_lot_of_template_parameters__and_the_other_interface_templates_don_t">iterator_interface 90 Takes a Lot of Template Parameters, And the Other Interface Templates Don't</a> 91 </h4> 92<p> 93 <code class="computeroutput"><a class="link" href="../boost/stl_interfaces/v1/iterator_interface.html" title="Struct template iterator_interface">iterator_interface</a></code> does in fact 94 take a lot of template parameters. However, it usually only takes three: the 95 <code class="computeroutput"><span class="identifier">Derived</span></code> type, the iterator 96 category, and the iterator's <code class="computeroutput"><span class="identifier">value_type</span></code>. 97 </p> 98<p> 99 When you make a proxy iterator, you typically use the <code class="computeroutput"><a class="link" href="../boost/stl_interfaces/v1/proxy_iterator_interface.html" title="Type definition proxy_iterator_interface">proxy_iterator_interface</a></code> alias, 100 and you again only need the same three template parameters. Though you can 101 opt into more template parameters, the rest are seldom necessary. 102 </p> 103<p> 104 By contrast, the <code class="computeroutput"><a class="link" href="../boost/stl_interfaces/v1/view_interface.html" title="Struct template view_interface">view_interface</a></code> and <code class="computeroutput"><a class="link" href="../boost/stl_interfaces/v1/sequ_1_3_39_11_2_5_1_1_1_1.html" title="Struct template sequence_container_interface">sequence_container_interface</a></code> 105 templates have very few template parameters. For <code class="computeroutput"><a class="link" href="../boost/stl_interfaces/v1/view_interface.html" title="Struct template view_interface">view_interface</a></code>, this is because 106 there are no member typedefs in the <code class="computeroutput"><span class="identifier">view</span></code> 107 concept. For <code class="computeroutput"><a class="link" href="../boost/stl_interfaces/v1/sequ_1_3_39_11_2_5_1_1_1_1.html" title="Struct template sequence_container_interface">sequence_container_interface</a></code>, 108 it was deemed ridiculous to create a template whose purpose is to reduce code 109 size, which takes 14 template parameters. 110 </p> 111<h4> 112<a name="boost_stlinterfaces.rationale.h4"></a> 113 <span class="phrase"><a name="boost_stlinterfaces.rationale._classname_alt__boost__stl_interfaces__v1__sequence_container_interface___code__phrase_role__identifier__sequence_container_interface__phrase___code___classname__does_not_deduce_nested_types_like__code__phrase_role__identifier__iterator__phrase___code_"></a></span><a class="link" href="rationale.html#boost_stlinterfaces.rationale._classname_alt__boost__stl_interfaces__v1__sequence_container_interface___code__phrase_role__identifier__sequence_container_interface__phrase___code___classname__does_not_deduce_nested_types_like__code__phrase_role__identifier__iterator__phrase___code_">sequence_container_interface 114 Does not Deduce Nested Types Like <code class="computeroutput"><span class="identifier">iterator</span></code></a> 115 </h4> 116<p> 117 <code class="computeroutput"><a class="link" href="../boost/stl_interfaces/v1/sequ_1_3_39_11_2_5_1_1_1_1.html" title="Struct template sequence_container_interface">sequence_container_interface</a></code> 118 could deduce some of the nested types required for a standard sequence container. 119 For instance, <code class="computeroutput"><span class="identifier">iterator</span></code> can 120 be deduced as <code class="computeroutput"><span class="keyword">decltype</span><span class="special">(*</span><span class="identifier">begin</span><span class="special">())</span></code>. 121 However, a type <code class="computeroutput"><span class="identifier">D</span></code> derived from 122 <code class="computeroutput"><a class="link" href="../boost/stl_interfaces/v1/sequ_1_3_39_11_2_5_1_1_1_1.html" title="Struct template sequence_container_interface">sequence_container_interface</a></code> 123 may need to use some of these nested types — like <code class="computeroutput"><span class="identifier">iterator</span></code> 124 — in its interface or implementation. If this is the case, those nested 125 types are not available early enough in the parse to be used in <code class="computeroutput"><span class="identifier">D</span></code>, if they come from deductions in <code class="computeroutput"><a class="link" href="../boost/stl_interfaces/v1/sequ_1_3_39_11_2_5_1_1_1_1.html" title="Struct template sequence_container_interface">sequence_container_interface</a></code>. 126 This leaves the user in the awkward position of defining the same nested type 127 with a different name that can be used within <code class="computeroutput"><span class="identifier">D</span></code>. 128 It seems better to leave these types for the user to define. 129 </p> 130<h4> 131<a name="boost_stlinterfaces.rationale.h5"></a> 132 <span class="phrase"><a name="boost_stlinterfaces.rationale._classname_alt__boost__stl_interfaces__v1__sequence_container_interface___code__phrase_role__identifier__sequence_container_interface__phrase___code___classname__does_not_support_associative_or_unordered_associative_containers"></a></span><a class="link" href="rationale.html#boost_stlinterfaces.rationale._classname_alt__boost__stl_interfaces__v1__sequence_container_interface___code__phrase_role__identifier__sequence_container_interface__phrase___code___classname__does_not_support_associative_or_unordered_associative_containers">sequence_container_interface 133 Does not Support Associative or Unordered Associative Containers</a> 134 </h4> 135<p> 136 That's right. Associative containers have an interface that assumes that they 137 are node-based containers. On modern hardware, node-based containers are not 138 very efficient, and I don't want to encourage people to write more of them. 139 Unordered associative containers have an interface that precludes open addressing. 140 I don't want to encourage more of that either. 141 </p> 142<h4> 143<a name="boost_stlinterfaces.rationale.h6"></a> 144 <span class="phrase"><a name="boost_stlinterfaces.rationale._classname_alt__boost__stl_interfaces__v1__sequence_container_interface___code__phrase_role__identifier__sequence_container_interface__phrase___code___classname__does_not_satisfy_the_allocator_aware_container_requirements"></a></span><a class="link" href="rationale.html#boost_stlinterfaces.rationale._classname_alt__boost__stl_interfaces__v1__sequence_container_interface___code__phrase_role__identifier__sequence_container_interface__phrase___code___classname__does_not_satisfy_the_allocator_aware_container_requirements">sequence_container_interface 145 Does not Satisfy the Allocator-Aware Container Requirements</a> 146 </h4> 147<p> 148 It may not be immediately obvious, but <code class="computeroutput"><a class="link" href="../boost/stl_interfaces/v1/sequ_1_3_39_11_2_5_1_1_1_1.html" title="Struct template sequence_container_interface">sequence_container_interface</a></code> 149 simply cannot help with the allocator-aware requirements. All of the allocator-aware 150 requirements but 3 are special members and constructors. A <a href="https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern" target="_top">CRTP</a> 151 base template is unable to provide those, based on the language rules. That 152 leaves the <code class="computeroutput"><span class="identifier">allocator_type</span></code> typedef, 153 which the user must provide; member <code class="computeroutput"><span class="identifier">swap</span><span class="special">()</span></code>, which is already a container requirement 154 (the allocator-aware table entry just specifies that member <code class="computeroutput"><span class="identifier">swap</span><span class="special">()</span></code> must be constant-time); and <code class="computeroutput"><span class="identifier">get_allocator</span><span class="special">()</span></code>, 155 which again is something the user must provide. 156 </p> 157<p> 158 Most of the difficulty of dealing with allocators has to do with the implementation 159 details of their use within your container. <code class="computeroutput"><a class="link" href="../boost/stl_interfaces/v1/sequ_1_3_39_11_2_5_1_1_1_1.html" title="Struct template sequence_container_interface">sequence_container_interface</a></code> 160 provides missing elements of a sequence container's interface, by calling user-provided 161 members of that same interface. It cannot help you with your container's implementation. 162 </p> 163</div> 164<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 165<td align="left"></td> 166<td align="right"><div class="copyright-footer">Copyright © 2019 T. Zachary Laine<p> 167 Distributed under the Boost Software License, Version 1.0. (See accompanying 168 file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) 169 </p> 170</div></td> 171</tr></table> 172<hr> 173<div class="spirit-nav"> 174<a accesskey="p" href="../boost/stl_interfaces/v2/view_interface.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../stl_interfaces.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../thread.html"><img src="../../../doc/src/images/next.png" alt="Next"></a> 175</div> 176</body> 177</html> 178