Compare commits

...

12 Commits

Author SHA1 Message Date
dd70baeb84 This commit was manufactured by cvs2svn to create branch
'compiler_supported_error_messages'.

[SVN r13249]
2002-03-22 12:16:42 +00:00
ce02fa0c58 Removed crazy mess up from previous change:
(the body of the test was commented out with #if 0)


[SVN r12454]
2002-01-23 12:40:44 +00:00
5f196d037f added missing "inherited" typedef
[SVN r12332]
2002-01-17 07:47:25 +00:00
2ab1b6705f Changed occurances of "tuples::get" to "get" to verify new forwarding code in tuples.hpp
[SVN r12291]
2002-01-13 12:12:48 +00:00
55a83b6408 Added forwarding get functions to solve problems with using::tuples::get statement.
[SVN r12290]
2002-01-13 12:09:39 +00:00
c89357006b reflecting the removal of src/tuple.cpp
[SVN r12023]
2001-12-12 22:20:42 +00:00
2fe366f263 reflecting the change to remove tuple.cpp, adding some thanks
[SVN r12022]
2001-12-12 22:17:09 +00:00
8b3b6efe24 not needed anymore
[SVN r12021]
2001-12-12 22:08:12 +00:00
2e825630f8 made the iomanipulator storage indexes static members of a class.
Now all code can be in header files.


[SVN r12020]
2001-12-12 21:35:41 +00:00
8408cc0bd5 thomas witt:s patch for icl501, adds a workaround for explicitly
qualified arguments
Also fixed length, so that it now works for empty tuples as well


[SVN r11722]
2001-11-16 23:27:08 +00:00
8f29dbe149 Thomas Witt's patch for icl501: adds better test coverage
+ tests for length template


[SVN r11721]
2001-11-16 23:24:43 +00:00
e531e7ce2e added a comment about performance penalties with some compilers
[SVN r11603]
2001-11-05 21:18:48 +00:00
8 changed files with 232 additions and 118 deletions

View File

@ -63,7 +63,6 @@ To compensate for this "deficiency", the Boost Tuple Library implement
<p>To use tuple input and output operators,
<pre><code>#include &quot;boost/tuple/tuple_io.hpp&quot;</code></pre>
and add the <code>libs/tuple/src/tuple.hpp</code> file to your project.
Both <code>tuple_io.hpp</code> and <code>tuple_comparison.hpp</code> include <code>tuple.hpp</code>.
@ -416,8 +415,9 @@ parseable.
<h2><a name = "performance">Performance</a></h2>
Tuples are efficient. All functions are small inlined one-liners and a decent compiler will eliminate any extra cost.
Particularly, there is no performance difference between this code:
All tuple access and construction functions are small inlined one-liners.
Therefore, a decent compiler can eliminate any extra cost of using tuples compared to using hand written tuple like classes.
Particularly, with a decent compiler there is no performance difference between this code:
<pre><code>class hand_made_tuple {
A a; B b; C c;
@ -439,6 +439,8 @@ and this code:
t.get&lt;0&gt;(); t.get&lt;1&gt;(); t.get&lt;2&gt;();
</code></pre>
<p>Note, that there are widely used compilers (e.g. bcc 5.5.1) which fail to optimize this kind of tuple usage.
</p>
<p>
Depending on the optimizing ability of the compiler, the tier mechanism may have a small performance penalty compared to using
non-const reference parameters as a mechanism for returning multiple values from a function.
@ -488,11 +490,10 @@ Below is a list of compilers and known problems with each compiler:
</table>
<h2><a name = "thanks">Acknowledgements</a></h2>
Gary Powell has been an indispensable helping hand. In particular, stream manipulators for tuples were his idea. Doug Gregor came up with a working version for MSVC. Thanks to Jeremy Siek, William Kempf, Jens Maurer for their help and suggestions.
The comments by Vesa Karvonen, John Max Skaller, Ed Brey, Beman Dawes and David Abrahams helped to improve the
Gary Powell has been an indispensable helping hand. In particular, stream manipulators for tuples were his idea. Doug Gregor came up with a working version for MSVC. Thanks to Jeremy Siek, William Kempf and Jens Maurer for their help and suggestions.
The comments by Vesa Karvonen, John Max Skaller, Ed Brey, Beman Dawes, David Abrahams and Hartmut Kaiser helped to improve the
library.
The idea for the tie mechanism came from an old usenet article by Ian McCulloch, where he proposed something similar for std::pairs.
<h2><a name = "references">References</a></h2>
<p>

View File

@ -67,6 +67,17 @@ template<class T> struct length;
namespace detail {
#ifdef BOOST_NO_EXPLICIT_FUNCTION_TEMPLATE_ARGUMENTS
template<int N> struct workaround_holder {};
# define BOOST_TUPLE_DUMMY_PARM , detail::workaround_holder<N>* = 0
# define BOOST_TUPLE_SINGLE_DUMMY_PARM detail::workaround_holder<N>* = 0
#else
# define BOOST_TUPLE_DUMMY_PARM
# define BOOST_TUPLE_SINGLE_DUMMY_PARM
#endif
// -- generate error template, referencing to non-existing members of this
// template is used to produce compilation errors intentionally
template<class T>
@ -196,7 +207,7 @@ template<int N, class HT, class TT>
inline typename access_traits<
typename element<N, cons<HT, TT> >::type
>::non_const_type
get(cons<HT, TT>& c) {
get(cons<HT, TT>& c BOOST_TUPLE_DUMMY_PARM) {
return detail::get_class<N>::template
get<
typename access_traits<
@ -211,7 +222,7 @@ template<int N, class HT, class TT>
inline typename access_traits<
typename element<N, cons<HT, TT> >::type
>::const_type
get(const cons<HT, TT>& c) {
get(const cons<HT, TT>& c BOOST_TUPLE_DUMMY_PARM) {
return detail::get_class<N>::template
get<
typename access_traits<
@ -219,9 +230,6 @@ get(const cons<HT, TT>& c) {
>::const_type>(c);
}
// -- the cons template --------------------------------------------------
template <class HT, class TT>
@ -346,7 +354,7 @@ struct cons<HT, null_type> {
typename access_traits<
typename element<N, cons>::type
>::non_const_type
get() {
get(BOOST_TUPLE_SINGLE_DUMMY_PARM) {
return boost::tuples::get<N>(*this);
}
@ -354,7 +362,7 @@ struct cons<HT, null_type> {
typename access_traits<
typename element<N, cons>::type
>::const_type
get() const {
get(BOOST_TUPLE_SINGLE_DUMMY_PARM) const {
return boost::tuples::get<N>(*this);
}
@ -367,6 +375,11 @@ struct length {
BOOST_STATIC_CONSTANT(int, value = 1 + length<typename T::tail_type>::value);
};
template<>
struct length<tuple<> > {
BOOST_STATIC_CONSTANT(int, value = 0);
};
template<>
struct length<null_type> {
BOOST_STATIC_CONSTANT(int, value = 0);
@ -753,6 +766,8 @@ tie(T1& t1, T2& t2, T3& t3, T4& t4, T5& t5, T6& t6, T7& t7, T8& t8,
} // end of namespace tuples
} // end of namespace boost
#undef BOOST_TUPLE_DUMMY_PARM
#undef BOOST_TUPLE_SINGLE_DUMMY_PARM
#endif // BOOST_TUPLE_BASIC_HPP

View File

@ -53,6 +53,21 @@ namespace tuples {
// a helper function to provide a const null_type type temporary
inline const null_type cnull_type() { return null_type(); }
// forward declaration of tuple
template<
typename T1 = null_type,
typename T2 = null_type,
typename T3 = null_type,
typename T4 = null_type,
typename T5 = null_type,
typename T6 = null_type,
typename T7 = null_type,
typename T8 = null_type,
typename T9 = null_type,
typename T10 = null_type
>
class tuple;
namespace detail {
// Takes a pointer and routes all assignments to whatever it points to
@ -263,6 +278,10 @@ namespace tuples {
{
BOOST_STATIC_CONSTANT(int, value = 1 + length<typename Tuple::tail_type>::value);
};
template<> struct length<tuple<> > {
BOOST_STATIC_CONSTANT(int, value = 0);
};
template<>
struct length<null_type>
@ -318,15 +337,15 @@ namespace tuples {
// tuple class
template<
typename T1,
typename T2 = null_type,
typename T3 = null_type,
typename T4 = null_type,
typename T5 = null_type,
typename T6 = null_type,
typename T7 = null_type,
typename T8 = null_type,
typename T9 = null_type,
typename T10 = null_type
typename T2,
typename T3,
typename T4,
typename T5,
typename T6,
typename T7,
typename T8,
typename T9,
typename T10
>
class tuple :
public detail::map_tuple_to_cons<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>::cons1
@ -345,6 +364,7 @@ namespace tuples {
typedef typename mapped_tuple::cons1 cons1;
public:
typedef cons1 inherited;
typedef tuple self_type;
explicit tuple(const T1& t1 = T1(),

View File

@ -37,7 +37,50 @@ namespace boost {
using tuples::tuple;
using tuples::make_tuple;
using tuples::tie;
#if !defined(BOOST_NO_USING_TEMPLATE)
using tuples::get;
#elif !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
//
// The "using tuples::get" statement causes the
// Borland compiler to ICE, use forwarding
// functions instead:
//
template<int N, class HT, class TT>
inline typename tuples::access_traits<
typename tuples::element<N, tuples::cons<HT, TT> >::type
>::non_const_type
get(tuples::cons<HT, TT>& c) {
return tuples::get<N,HT,TT>(c);
}
// get function for const cons-lists, returns a const reference to
// the element. If the element is a reference, returns the reference
// as such (that is, can return a non-const reference)
template<int N, class HT, class TT>
inline typename tuples::access_traits<
typename tuples::element<N, tuples::cons<HT, TT> >::type
>::const_type
get(const tuples::cons<HT, TT>& c) {
return tuples::get<N,HT,TT>(c);
}
#else // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
//
// MSVC, using declarations don't mix with templates well,
// so use forwarding functions instead:
//
template<int N, typename Head, typename Tail>
typename tuples::detail::element_ref<N, tuples::cons<Head, Tail> >::RET
get(tuples::cons<Head, Tail>& t, tuples::detail::workaround_holder<N>* = 0)
{
return tuples::detail::get_class<N>::get(t);
}
template<int N, typename Head, typename Tail>
typename tuples::detail::element_const_ref<N, tuples::cons<Head, Tail> >::RET
get(const tuples::cons<Head, Tail>& t, tuples::detail::workaround_holder<N>* = 0)
{
return tuples::detail::get_class<N>::get(t);
}
#endif // BOOST_NO_USING_TEMPLATE
} // end namespace boost

View File

@ -45,11 +45,19 @@ namespace detail {
class format_info {
public:
enum manipulator_type { open, close, delimiter };
BOOST_STATIC_CONSTANT(int, number_of_manipulators = delimiter + 1);
private:
static const int stream_index[number_of_manipulators];
static int get_stream_index (int m)
{
static const int stream_index[number_of_manipulators]
= { std::ios::xalloc(), std::ios::xalloc(), std::ios::xalloc() };
return stream_index[m];
}
format_info(const format_info&);
format_info();
@ -58,7 +66,7 @@ public:
#if defined (BOOST_NO_TEMPLATED_STREAMS)
static char get_manipulator(std::ios& i, manipulator_type m) {
char c = static_cast<char>(i.iword(stream_index[m]));
char c = static_cast<char>(i.iword(get_stream_index(m)));
// parentheses and space are the default manipulators
if (!c) {
@ -72,7 +80,7 @@ public:
}
static void set_manipulator(std::ios& i, manipulator_type m, char c) {
i.iword(stream_index[m]) = static_cast<long>(c);
i.iword(get_stream_index(m)) = static_cast<long>(c);
}
#else
template<class CharType, class CharTrait>
@ -82,7 +90,7 @@ public:
// A valid instanitation of basic_stream allows CharType to be any POD,
// hence, the static_cast may fail (it fails if long is not convertible
// to CharType
CharType c = static_cast<CharType>(i.iword(stream_index[m]) );
CharType c = static_cast<CharType>(i.iword(get_stream_index(m)) );
// parentheses and space are the default manipulators
if (!c) {
switch(m) {
@ -102,7 +110,7 @@ public:
// A valid instanitation of basic_stream allows CharType to be any POD,
// hence, the static_cast may fail (it fails if CharType is not
// convertible long.
i.iword(stream_index[m]) = static_cast<long>(c);
i.iword(get_stream_index(m)) = static_cast<long>(c);
}
#endif // BOOST_NO_TEMPLATED_STREAMS
};

View File

@ -1,34 +0,0 @@
// tuple.cpp -----------------------------------------------------
// Copyright (C) 1999, 2000, 2001 Jaakko J<>rvi (jaakko.jarvi@cs.utu.fi)
// Copyright (C) 2001 Gary Powell (gary.powell@sierra.com)
//
// Permission to copy, use, sell and distribute this software is granted
// provided this copyright notice appears in all copies.
// Permission to modify the code and to distribute modified code is granted
// provided this copyright notice appears in all copies, and a notice
// that the code was modified is included with the copyright notice.
//
// This software is provided "as is" without express or implied warranty,
// and with no claim as to its suitability for any purpose.
// For more information, see http://lambda.cs.utu.fi
// Revision History
// 16 02 01 Initial Version (GWP)
// -----------------------------------------------------------------
#include "boost/tuple/tuple_io.hpp"
namespace boost {
namespace tuples {
namespace detail {
const int
format_info::stream_index[number_of_manipulators]
= { std::ios::xalloc(), std::ios::xalloc(), std::ios::xalloc() };
} // namespace detail
} // namespace tuples
} // namespace boost

View File

@ -8,7 +8,9 @@ For example, in libs/tuple/test directory you would type (using g++):
g++ -I../../.. tuple_test_bench.cpp
If you want to use tuple_io, you need to compile and link src/tuple.cpp:
The following is not true anymore:
g++ -I../../.. ../src/tuple.cpp io_test.cpp
If you want to use tuple_io, you need to compile and link src/tuple.cpp:
g++ -I../../.. ../src/tuple.cpp io_test.cpp
Thanks to Hartmut Kaiser's suggestion, the tuple.cpp is not needed anymore.

View File

@ -97,42 +97,42 @@ tuple<char(&)[10]> v2(cs); // ok
void
construction_test()
{
// Note, the get function can be called without the tuples:: qualifier,
// as it is lifted to namespace boost with a "using tuples::get" but
// MSVC 6.0 just cannot find get without the namespace qualifier
tuple<int> t1;
BOOST_TEST(tuples::get<0>(t1) == int());
tuple<int> t1;
BOOST_TEST(get<0>(t1) == int());
tuple<float> t2(5.5f);
BOOST_TEST(tuples::get<0>(t2) > 5.4f && tuples::get<0>(t2) < 5.6f);
BOOST_TEST(get<0>(t2) > 5.4f && get<0>(t2) < 5.6f);
tuple<foo> t3(foo(12));
BOOST_TEST(tuples::get<0>(t3) == foo(12));
BOOST_TEST(get<0>(t3) == foo(12));
tuple<double> t4(t2);
BOOST_TEST(tuples::get<0>(t4) > 5.4 && tuples::get<0>(t4) < 5.6);
BOOST_TEST(get<0>(t4) > 5.4 && get<0>(t4) < 5.6);
tuple<int, float> t5;
BOOST_TEST(tuples::get<0>(t5) == int());
BOOST_TEST(tuples::get<1>(t5) == float());
BOOST_TEST(get<0>(t5) == int());
BOOST_TEST(get<1>(t5) == float());
tuple<int, float> t6(12, 5.5f);
BOOST_TEST(tuples::get<0>(t6) == 12);
BOOST_TEST(tuples::get<1>(t6) > 5.4f && tuples::get<1>(t6) < 5.6f);
BOOST_TEST(get<0>(t6) == 12);
BOOST_TEST(get<1>(t6) > 5.4f && get<1>(t6) < 5.6f);
tuple<int, float> t7(t6);
BOOST_TEST(tuples::get<0>(t7) == 12);
BOOST_TEST(tuples::get<1>(t7) > 5.4f && tuples::get<1>(t7) < 5.6f);
BOOST_TEST(get<0>(t7) == 12);
BOOST_TEST(get<1>(t7) > 5.4f && get<1>(t7) < 5.6f);
tuple<long, double> t8(t6);
BOOST_TEST(tuples::get<0>(t8) == 12);
BOOST_TEST(tuples::get<1>(t8) > 5.4f && tuples::get<1>(t8) < 5.6f);
BOOST_TEST(get<0>(t8) == 12);
BOOST_TEST(get<1>(t8) > 5.4f && get<1>(t8) < 5.6f);
dummy(
dummy(
tuple<no_def_constructor, no_def_constructor, no_def_constructor>(
std::string("Jaba"), // ok, since the default
std::string("Jaba"), // ok, since the default
std::string("Daba"), // constructor is not used
std::string("Doo")
)
@ -141,7 +141,7 @@ construction_test()
// testing default values
dummy(tuple<int, double>());
dummy(tuple<int, double>(1));
dummy(tuple<int, double>(1,3.14));
dummy(tuple<int, double>(1,3.14));
// dummy(tuple<double&>()); // should fail, not defaults for references
@ -154,9 +154,8 @@ construction_test()
dummy(tuple<const double&>(dd+3.14)); // ok, but dangerous
#endif
// dummy(tuple<double&>(dd+3.14)); // should fail,
// dummy(tuple<double&>(dd+3.14)); // should fail,
// // temporary to non-const reference
}
@ -164,36 +163,68 @@ construction_test()
// - testing element access ---------------------------------------------------
// ----------------------------------------------------------------------------
void element_access_test()
void element_access_test()
{
#if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
double d = 2.7;
double d = 2.7;
A a;
tuple<int, double&, const A&> t(1, d, a);
const tuple<int, double&, const A> ct = t;
tuple<int, double&, const A&, int> t(1, d, a, 2);
const tuple<int, double&, const A, int> ct = t;
int i = tuples::get<0>(t);
int j = tuples::get<0>(ct);
BOOST_TEST(i == 1 && j == 1);
int i = get<0>(t);
int i2 = get<3>(t);
BOOST_TEST(i == 1 && i2 == 2);
int j = get<0>(ct);
BOOST_TEST(j == 1);
tuples::get<0>(t) = 5;
get<0>(t) = 5;
BOOST_TEST(t.head == 5);
// tuples::get<0>(ct) = 5; // can't assign to const
// get<0>(ct) = 5; // can't assign to const
double e = tuples::get<1>(t);
double e = get<1>(t);
BOOST_TEST(e > 2.69 && e < 2.71);
tuples::get<1>(t) = 3.14+i;
BOOST_TEST(tuples::get<1>(t) > 4.13 && tuples::get<1>(t) < 4.15);
get<1>(t) = 3.14+i;
BOOST_TEST(get<1>(t) > 4.13 && get<1>(t) < 4.15);
// tuples::get<4>(t) = A(); // can't assign to const
// dummy(tuples::get<5>(ct)); // illegal index
// get<4>(t) = A(); // can't assign to const
// dummy(get<5>(ct)); // illegal index
++tuples::get<0>(t);
BOOST_TEST(tuples::get<0>(t) == 6);
++get<0>(t);
BOOST_TEST(get<0>(t) == 6);
dummy(i); dummy(j); dummy(e); // avoid warns for unused variables
dummy(i); dummy(i2); dummy(j); dummy(e); // avoid warns for unused variables
#else
double d = 2.7;
A a;
tuple<int, double, const A, int> t(1, d, a, 2);
int i = get<0>(t);
int i2 = get<3>(t);
BOOST_TEST(i == 1 && i2 == 2);
get<0>(t) = 5;
BOOST_TEST(t.head == 5);
// get<0>(ct) = 5; // can't assign to const
double e = get<1>(t);
BOOST_TEST(e > 2.69 && e < 2.71);
get<1>(t) = 3.14+i;
BOOST_TEST(get<1>(t) > 4.13 && get<1>(t) < 4.15);
// get<4>(t) = A(); // can't assign to const
// dummy(get<5>(ct)); // illegal index
++get<0>(t);
BOOST_TEST(get<0>(t) == 6);
dummy(i); dummy(i2); dummy(e); // avoid warns for unused variables
#endif
}
@ -210,13 +241,13 @@ copy_test()
tuple<int, char> t1(4, 'a');
tuple<int, char> t2(5, 'b');
t2 = t1;
BOOST_TEST(tuples::get<0>(t1) == tuples::get<0>(t2));
BOOST_TEST(tuples::get<1>(t1) == tuples::get<1>(t2));
BOOST_TEST(get<0>(t1) == get<0>(t2));
BOOST_TEST(get<1>(t1) == get<1>(t2));
tuple<long, std::string> t3(2, "a");
t3 = t1;
BOOST_TEST((double)tuples::get<0>(t1) == tuples::get<0>(t3));
BOOST_TEST(tuples::get<1>(t1) == tuples::get<1>(t3)[0]);
BOOST_TEST((double)get<0>(t1) == get<0>(t3));
BOOST_TEST(get<1>(t1) == get<1>(t3)[0]);
// testing copy and assignment with implicit conversions between elements
// testing tie
@ -237,15 +268,15 @@ void
mutate_test()
{
tuple<int, float, bool, foo> t1(5, 12.2f, true, foo(4));
tuples::get<0>(t1) = 6;
tuples::get<1>(t1) = 2.2f;
tuples::get<2>(t1) = false;
tuples::get<3>(t1) = foo(5);
get<0>(t1) = 6;
get<1>(t1) = 2.2f;
get<2>(t1) = false;
get<3>(t1) = foo(5);
BOOST_TEST(tuples::get<0>(t1) == 6);
BOOST_TEST(tuples::get<1>(t1) > 2.1f && tuples::get<1>(t1) < 2.3f);
BOOST_TEST(tuples::get<2>(t1) == false);
BOOST_TEST(tuples::get<3>(t1) == foo(5));
BOOST_TEST(get<0>(t1) == 6);
BOOST_TEST(get<1>(t1) > 2.1f && get<1>(t1) < 2.3f);
BOOST_TEST(get<2>(t1) == false);
BOOST_TEST(get<3>(t1) == foo(5));
}
// ----------------------------------------------------------------------------
@ -256,13 +287,13 @@ void
make_tuple_test()
{
tuple<int, char> t1 = make_tuple(5, 'a');
BOOST_TEST(tuples::get<0>(t1) == 5);
BOOST_TEST(tuples::get<1>(t1) == 'a');
BOOST_TEST(get<0>(t1) == 5);
BOOST_TEST(get<1>(t1) == 'a');
tuple<int, std::string> t2;
t2 = make_tuple((short int)2, std::string("Hi"));
BOOST_TEST(tuples::get<0>(t2) == 2);
BOOST_TEST(tuples::get<1>(t2) == "Hi");
BOOST_TEST(get<0>(t2) == 2);
BOOST_TEST(get<1>(t2) == "Hi");
#if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
@ -415,10 +446,30 @@ void cons_test()
void const_tuple_test()
{
const tuple<int, float> t1(5, 3.3f);
BOOST_TEST(tuples::get<0>(t1) == 5);
BOOST_TEST(tuples::get<1>(t1) == 3.3f);
BOOST_TEST(get<0>(t1) == 5);
BOOST_TEST(get<1>(t1) == 3.3f);
}
// ----------------------------------------------------------------------------
// - testing length -----------------------------------------------------------
// ----------------------------------------------------------------------------
void tuple_length_test()
{
typedef tuple<int, float, double> t1;
using tuples::cons;
typedef cons<int, cons< float, cons <double, tuples::null_type> > > t1_cons;
typedef tuple<> t2;
typedef tuples::null_type t3;
BOOST_STATIC_ASSERT(tuples::length<t1>::value == 3);
BOOST_STATIC_ASSERT(tuples::length<t1_cons>::value == 3);
BOOST_STATIC_ASSERT(tuples::length<t2>::value == 0);
BOOST_STATIC_ASSERT(tuples::length<t3>::value == 0);
}
// ----------------------------------------------------------------------------
// - main ---------------------------------------------------------------------
@ -436,5 +487,13 @@ int test_main(int, char *[]) {
ordering_test();
cons_test();
const_tuple_test();
tuple_length_test();
return 0;
}