Boost logo

Boost :

From: John Maddock (john_at_[hidden])
Date: 2006-06-04 11:51:09


AlisdairM wrote:
> I finally committed support for zero length arrays to the boost array
> library, and it is showing up what I believe are test case errors in
> the TR1 'tricky' tests.
>
> [tuple interface remains available only through John's TR1
> implementation right now]
>
>
> The following tests from test_array_trick.cpp lines should be
> reconsidered:
>
> line 51: empty will return true if the array is length zero.
> verify_return_type(ca.empty(), false);

It only checks that the return *type* is a bool: it doesn't care what the
value is, so IMO it's correct.

> lines 73-75: are all compile-time fails for array length zero
> BOOST_STATIC_ASSERT((::boost::is_same< typename
> std::tr1::tuple_element<0,T>::type, value_type>::value));
> verify_return_type(&std::tr1::get<0>(a), static_cast<value_type*>(0));
> verify_return_type(&std::tr1::get<0>(ca), static_cast<const
> value_type*>(0));

Yup, Fixed.

> I thought about the tests for operator[]/at/front/back and figure
> these are OK, even the the results of calling those functions are
> unspecified (but not undefined) as:
> i/ you don't actually call the functions!
> ii/ they are required to be present, so this code MUST compile
> iii/ I believe even if the effects of calling the fns is unspecified,
> the actual return type is fixed.
>
> So those tests are good, indeed they must be kept!
>
>
> With the above mods, I believe we should now pass the TR1 tricky tests
> for array, and I will leave back-porting the tuple interface to
> namespace boost for 1.35.

Unfortunately that doesn't quite get the test case compiling: your stub
implementations of front() back() etc all need to return a value to actually
compile (this is with VC++ anyway), patch follows below, let me know if it's
OK to commit.

John.

Index: boost/array.hpp
===================================================================
RCS file: /cvsroot/boost/boost/boost/array.hpp,v
retrieving revision 1.25
diff -u -r1.25 array.hpp
--- boost/array.hpp 4 Jun 2006 11:01:59 -0000 1.25
+++ boost/array.hpp 4 Jun 2006 15:46:30 -0000
@@ -30,6 +30,7 @@

 // Handles broken standard libraries better than <iterator>
 #include <boost/detail/iterator.hpp>
+#include <boost/throw_exception.hpp>
 #include <algorithm>

 // FIXES for broken compilers
@@ -212,37 +213,43 @@
         {
             BOOST_ASSERT( "out of range" );
             failed_rangecheck();
+ return null_item();
         }

         const_reference operator[](size_type i) const
         {
             BOOST_ASSERT( "out of range" );
             failed_rangecheck();
+ return null_item();
         }

         // at() with range check
- reference at(size_type i) { failed_rangecheck(); }
- const_reference at(size_type i) const { failed_rangecheck(); }
+ reference at(size_type i) { failed_rangecheck();
return
 null_item(); }
+ const_reference at(size_type i) const { failed_rangecheck();
return
 null_item(); }

         // front() and back()
         reference front()
         {
             failed_rangecheck();
+ return null_item();
         }

         const_reference front() const
         {
             failed_rangecheck();
+ return null_item();
         }

         reference back()
         {
             failed_rangecheck();
+ return null_item();
         }

         const_reference back() const
         {
             failed_rangecheck();
+ return null_item();
         }

         // size is constant
@@ -272,8 +279,19 @@

         // check range (may be private because it is static)
         static void failed_rangecheck () {
- throw std::range_error("attempt to access element of an
empty a
rray");
+ std::range_error e("attempt to access element of an empty
array
");
+ boost::throw_exception(e);
             }
+ static reference null_item()
+ {
+ //
+ // This function must exist to allow our various interfaces to
+ // actually compile, however it will never be called, because
+ // an exception will always be thrown before we get here.
+ //
+ static T placeholder;
+ return placeholder;
+ }
     };
 #endif


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk