Boost logo

Boost :

From: Synge Todo (wistaria_at_[hidden])
Date: 2002-04-03 10:48:14


From: Douglas Gregor <gregod_at_[hidden]>
> type_with_alignment<> is available in CVS. I'm guessing we should also add
> aligned_storage somewhere, because I'm sure quite a few of us will use it:

I'm now rewriting my fixed-capacity librariy on top of
type_with_alignment<>. (I will resubmit them once I have finished
revising the documentations.)

In my fixed-capacity library I have also prepeared an aligned-storage
class. My implementation, which has minimum member functions as a
container, is given below.

Synge Todo
wistaria_at_[hidden]

-------------------------------------------------------------------------
// uninitialized_array.hpp

// Copyright (C) 2002 Synge Todo <wistaria_at_[hidden]>
//
// Permission to copy, use, modify, sell and distribute this software
// is granted provided this copyright notice appears in all copies.
// This software is provided "as is" without express or implied
// warranty, and with no claim as to its suitability for any purpose.

// See http://www.boost.org for updates, documentation, and revision history.

#ifndef UNINITIALIZED_ARRAY_HPP
#define UNINITIALIZED_ARRAY_HPP

#include <boost/config.hpp>
#include <boost/static_assert.hpp>
#include <boost/type_traits/alignment_traits.hpp>
#include <cstddef>

namespace boost {

// class template uninitialized_array ---------------------------------------//

template<class T, std::size_t N>
class uninitialized_array
{
public:
  // types:
  typedef std::size_t size_type;
  typedef T value_type;
  typedef T& reference;
  typedef const T& const_reference;
  typedef T* iterator;
  typedef const T* const_iterator;

  BOOST_STATIC_CONSTANT(size_type, static_size = N);

  // compiler-generated constructors/destructor are fine

  // iterators:
  iterator begin() { return reinterpret_cast<iterator>(buffer_); }
  const_iterator begin() const {
    return reinterpret_cast<const_iterator>(buffer_);
  }
  iterator end() { return begin() + N; }
  const_iterator end() const { return begin() + N; }

  // capacity:
  static size_type size() { return N; }

  // element access:
  reference operator[](size_type i) { return *(begin() + i); }
  const_reference operator[](size_type i) const { return *(begin() + i); }
  
private:
  BOOST_STATIC_ASSERT(N > 0);

  union {
    char buffer_[N * sizeof(T)];
    typename boost::type_with_alignment<boost::alignment_of<T>::value>::type
      dummy_;
  };

}; // uninitialized_array

} // end namespace boost

#endif // UNINITIALIZED_ARRAY_HPP


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