[Home]Deprecate Smart Array Extend Smart Ptr - Smart Ptr Sugg

BOOST WIKI | RecentChanges | Preferences | Page List | Links List

No diff available--this is the first major revision. (minor diff)
Back to Suggestions - Smart ptr library


Deprecate smart_array extend smart_ptr

Is it possible to deprecate shared_array<T> and scoped_array<T> and replace them by shared_ptr<T[]> and scoped_ptr<T[]> respectively? The move_ptr library uses this strategy to create a more monolithic solution.

Changes to shared_ptr

template <class T>
class shared_ptr
{
private:
    BOOST_STATIC_CONSTANT(bool, is_array = boost::is_array<T>::value);

public:
   ...

   reference operator*() const // never throws
   {
      BOOST_STATIC_ASSERT(!is_array);
      BOOST_ASSERT(px != 0);
      return *px;
   }

   pointer operator->() const // never throws
   {
      BOOST_STATIC_ASSERT(!is_array);
      BOOST_ASSERT(px != 0);
      return px;
   }

   reference operator[](std::ptrdiff_t i) const // never throws
   {
      BOOST_STATIC_ASSERT(is_array);
      BOOST_ASSERT(px != 0);
      BOOST_ASSERT(i >= 0);
      return px[i];
   }
};

Example usage

#include <boost/shared_ptr.hpp>

struct foo
{
   int val;
};

void bar()
{
   boost::shared_ptr<foo> ptr(new foo);
   boost::shared_ptr<foo[]> array(new foo[1]);

   ptr->val = 1;
   (*ptr).val = 2;
   // ptr[0].val = 3; // Compiler error, BOOST_STATIC_ASSERT(is_array).

   // array->val = 1; // Compiler error, BOOST_STATIC_ASSERT(!is_array).
   // (*array).val = 2; // Compiler error, BOOST_STATIC_ASSERT(!is_array).
   array[0].val = 3;
}

References


    - People/Anders Dalvander

BOOST WIKI | RecentChanges | Preferences | Page List | Links List
Edit text of this page | View other revisions
Last edited June 2, 2005 3:42 am (diff)
Search:
Disclaimer: This site not officially maintained by Boost Developers