Boost logo

Boost :

From: Fernando Cacciola (fcacciola_at_[hidden])
Date: 2001-09-04 14:27:48


A few days ago I presented class optional<>.
Peter Dimov recognized an error in the implementation and Douglas Gregor
uploaded a part of his variant<> class which I could use in optional<> to
solve the implementation error.

Initially, I couldn't compile Douglas code because Borland C++ 5.5 kept
crashing with an internal compiler error.

Fortunately, I was able to adapt the code.
There were a number of different strange problems.

First, although the borland compiler does recognize:
  static const int v = 1 ;
it seems to treat it differently than:
  enum { v = 1 } ;
since the former can't be used as a non-type template parameter while the
later can.

This forced me to wrap constants defined with BOOST_STATIC_CONSTANT in
an enum.

Second, borland inexplicably crashes if you declare an unnamed union with a
char array in it (!?)
So I had to 'name' the union in the final class.

Finally, the big trouble is that the handling of recursive non-type template
parameters seems to be broken. I had to turn every non-type parameter into a
type-parameter to make it work.

By the way, I introduced a couple of helper classes to do the
transformation:

    template<int N> struct typed_value
      { BOOST_STATIC_CONSTANT(int, value = N ) ; } ;

    template<int N> struct to_type
      { typedef typed_value<N> type ; } ;

Which are used like this:

    wrap -> typedef typename to_type<3>::type wrapped ;
    unwrap-> static const int value = wrapped::value ;

Perhaps these classes could be put in utility.hpp

Using Douglas's code I defined class aligned_storage<T>, which can be used
as:

void foo()
{
  aligned_storage<T> buffer ;
  T* ptr = new (buffer.address()) T(...);
}

I packed this in a file aligned_storage.hpp which I uploaded under 'class
optional' in the Files Section.

Since this is actually Doug's work, his name appears in the copyright,
following by mine.
(Are you OK with it?)

I also updated optional.hpp (and the test bed) to reflect this change an
other issues I've taken care of.
The header file is fully documented and the test bed has been extended to
test more situations.

Fernando Cacciola
Sierra s.r.l.
fcacciola_at_[hidden]
www.gosierra.com


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