mirror of
https://github.com/boostorg/tuple.git
synced 2026-04-29 02:03:23 +02:00
00688f9a4a
[SVN r10834]
133 lines
6.2 KiB
HTML
133 lines
6.2 KiB
HTML
<html>
|
|
|
|
<title>Design decisions rationale for Boost Tuple Library</title>
|
|
|
|
<body bgcolor="#FFFFFF" text="#000000">
|
|
|
|
<IMG SRC="../../../c++boost.gif"
|
|
ALT="C++ Boost" width="277" height="86">
|
|
|
|
<h1>Tuple Library : design decisions rationale</h1>
|
|
|
|
<h2>About namespaces</h2>
|
|
|
|
<p>
|
|
There was a discussion about whether tuples should be in a separate namespace or directly at the <code>boost</code> namespace.
|
|
The common principle is that domain libraries (like <i>graph</i>, <i>python</i>) should be on a separate
|
|
sub-namespace, while utility like libraries directly in the <code>boost</code> namespace.
|
|
Tuples are somewhere in between, as the tuple template is clearly a general utility, but the library introduces quite a lot of names in addition to just the tuple template.
|
|
As a result of the discussion, tuple definitions are now directly under the <code>boost</code> namespace.
|
|
<p>
|
|
|
|
<h4>For those who are really interested in namespaces</h4>
|
|
|
|
<p>
|
|
Note! The following discussion is not relevant for the Tuple library, as the 'no
|
|
sub-namespace' decision was taken, but it may be useful for other library writers.
|
|
</p>
|
|
<p>
|
|
In the original tuple library submission, all names were under the namespace <code>tuples</code>. This brought up the issue of naming
|
|
sub-namespaces.
|
|
The rationale for not using the most natural name 'tuple' was to avoid having an identical name with the tuple template. Namespace names are, however, not generally in plural form in boost libraries. Further, no real trouble was reported for using the same name for a namespace and a class.
|
|
But we found some trouble after all.
|
|
One solution proposed to the dilemma of introducing a sub-namespace or not was as follows: use a
|
|
sub-namespace but lift the most common names to the <code>boost</code> namespace with using declarations.
|
|
Both gcc and edg compilers rejected such using declarations if the namespace and class names were identical:
|
|
|
|
<code><pre>namespace boost {
|
|
namespace tuple {
|
|
class cons;
|
|
class tuple;
|
|
...
|
|
}
|
|
using tuple::cons; // ok
|
|
using tuple::tuple; // error
|
|
...
|
|
}
|
|
</pre></code>
|
|
|
|
|
|
Note, however, that a corresponding using declaration in the global namespace seemed to be ok:
|
|
|
|
<code><pre>
|
|
using boost::tuple::tuple; // ok;
|
|
</pre></code>
|
|
|
|
|
|
<h2>The end mark of the cons list (nil, null_type, ...)</h2>
|
|
|
|
<p>
|
|
Tuples are internally represented as <code>cons</code> lists:
|
|
|
|
<code><pre>tuple<int, int>
|
|
</pre></code>
|
|
inherits from
|
|
<code><pre>cons<int, cons<int, null_type> >
|
|
</code></pre>
|
|
|
|
<code>null_type</code> is the end mark of the list. Original proposition was <code>nil</code>, but the name is used in MacOS, and might have caused problems, so <code>null_type</code> was chosen instead.
|
|
Other names considered were <i>null_t</i> and <i>unit</i> (the empty tuple type in SML).
|
|
<p>
|
|
Note that <code>null_type</code> is the internal representation of an empty tuple: <code>tuple<></code> inherits from <code>null_type</code>.
|
|
</p>
|
|
|
|
<h2>Element indexing</h2>
|
|
|
|
<p>
|
|
Whether to use 0- or 1-based indexing was discussed more than thoroughly, and the following observations were made:
|
|
|
|
<ul>
|
|
<li> 0-based indexing is 'the C++ way' and used with arrays etc.</li>
|
|
<li> 1-based 'name like' indexing exists as well, eg. <code>bind1st</code>, <code>bind2nd</code>, <code>pair::first</code>, etc.</li>
|
|
</ul>
|
|
Tuple access with the syntax <code>get<N>(a)</code>, or <code>a.get<N>()</code> (where <code>a</code> is a tuple and <code>N</code> an index), was considered to be of the first category, hence, the index of the first element in a tuple is 0.
|
|
|
|
<p>
|
|
A suggestion to provide 1-based 'name like' indexing with constants like <code>_1st</code>, <code>_2nd</code>, <code>_3rd</code>, ... was made.
|
|
By suitably chosen constant types, this would allow alternative syntaxes:
|
|
|
|
<code><pre>a.get<0>() == a.get(_1st) == a[_1st] == a(_1st);
|
|
</pre></code>
|
|
|
|
We chose not to provide more than one indexing method for the following reasons:
|
|
<ul>
|
|
<li>0-based indexing might not please everyone, but once its fixed, it is less confusing than having two different methods (would anyone want such constants for arrays?).</li>
|
|
<li>Adding the other indexing scheme doesn't really provide anything new (like a new feature) to the user of the library.</li>
|
|
<li>C++ variable and constant naming rules don't give many possibilities for defining short and nice index constants (like <code>_1st</code>, ...).
|
|
Let the binding and lambda libraries use these for a better purpose.</li>
|
|
<li>The access syntax <code>a[_1st]</code> (or <code>a(_1st)</code>) is appealing, and almost made us add the index constants after all. However, 0-based subscripting is so deep in C++, that we had a fear for confusion.</li>
|
|
<li>
|
|
Such constants are easy to add.
|
|
</li>
|
|
</ul>
|
|
|
|
|
|
<h2>Tuple comparison</h2>
|
|
|
|
The comparison operator implements lexicographical order.
|
|
Other orderings were considered, mainly dominance (<i>a < b iff for each i a(i) < b(i)</i>).
|
|
Our belief is, that lexicographical ordering, though not mathematically the most natural one, is the most frequently needed ordering in everyday programming.
|
|
|
|
<h2>Streaming</h2>
|
|
|
|
<p>
|
|
The characters specified with tuple stream manipulators are stored within the space allocated by <code>ios_base::xalloc</code>, which allocates storage for <code>long</code> type objects.
|
|
<code>static_cast</code> is used in casting between <code>long</code> and the stream's character type.
|
|
Streams that have character types not convertible back and forth to long thus fail to compile.
|
|
|
|
This may be revisited at some point. The two possible solutions are:
|
|
<ul>
|
|
<li>Allow only plain <code>char</code> types as the tuple delimiters and use <code>widen</code> and <code>narrow</code> to convert between the real character type of the stream.
|
|
This would always compile, but some calls to set manipulators might result in a different
|
|
character than expected (some default character).</li>
|
|
<li>Allocate enough space to hold the real character type of the stream.
|
|
This means memory for holding the delimiter characters must be allocated separately, and that pointers to this memory are stored in the space allocated with <code>ios_base::xalloc</code>.
|
|
Any volunteers?</li>
|
|
</ul>
|
|
|
|
<A href="tuple_users_guide.html">Back to the user's guide</A>
|
|
<hr><p>© Copyright Jaakko Järvi 2001.
|
|
</body>
|
|
</html>
|
|
|