Re: What are the differences between std::shared_ptr and boost::shared_ptr?
On 14 Sep., 09:47, Martin B. wrote:
On 14.09.2010 08:49, SG wrote:
On 14 Sep., 07:21, Johannes Schaub wrote:
Can someone please report where std::shared_array<> is?
I guess we don't need it since we can do things like
auto sa = std::make_shared<std::vector<int>>(100);
(*sa)[5] = 1729;
Horrible!
It's not *that* bad. Alternatives:
sa->at(5) = 1729;
or
auto& vecref = *sa;
vecref[5] = 1729;
or you could write a simple class that wraps such a shared_ptr and
forwards operator[], begin, end, etc to the vector.
In case you didn't refer to the syntax but to the memory layout, I
think boost::shared_array<X> and std::shared_ptr<vector<X>> (when
created with make_shared) probably leads to something very similar.
You have two allocations in both cases.
boost::shared_array<int> sa1 (new int[100]);
allocates an int-array and some control block for the reference
counter and deleter.
auto sa2 = std::make_shared<std::vector<int>>(100);
also allocates an int-array and some control block. But this time, the
control block not only stores the ref counter and deleter but also the
vector<int> object's value representation. Still, only two allocations
when implemented well.
and
unique_ptr<int[]> ua (new int[100]);
ua[5] = 1729;
Does this work with shared_ptr too?
No.
Cheers!
SG