Boost logo

Boost Users :

From: Tim Day (timday_at_[hidden])
Date: 2007-05-31 18:02:09


The ability to assign a shared_ptr<T> to shared_ptr<const T> is useful
for controlling which parts of software have write access to the shared
object, so I'm surprised that while e.g

  boost::shared_ptr<int> sp(new int);
  boost::shared_ptr<const int> csp=sp;

compiles fine, and prohibits e.g *csp=0;, the equivalent for
shared_array doesn't compile:

  boost::shared_array<int> sa(new int[10]);
  boost::shared_array<const int> csa=sa; // not allowed

[The gcc 4.1.2 error is "conversion from ‘boost::shared_array<int>’ to
non-scalar type ‘boost::shared_array<const int>’ requested"]

I'm looking to prevent modification of elements of csa of course.
The obvious alternative is to use a shared_ptr to a std::vector of
course:

  boost::shared_ptr<std::vector<int> > sv(new std::vector<int>(10));
  boost::shared_ptr<const std::vector<int> > csv=sv; // fine
  // (*csv)[0]=0; // prevented by const, as intended

However, it's unclear to me exactly why the shared_array of const
doesn't work as expected. Am I missing some necessary detail to make it
work, is shared_array simply less featureful (the header is certainly
shorter) or is there some reason it can't work ?

Thanks for any explanation
Tim
[I'm using boost 1.33.1]


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net