Boost logo

Boost :

Subject: Re: [boost] make_shared_array
From: Peter Dimov (lists_at_[hidden])
Date: 2012-11-02 20:20:12

Nathan Crookston wrote:
> On Fri, Nov 2, 2012 at 11:47 AM, Peter Dimov <lists_at_[hidden]> wrote:
> >
> > Voila:
> >
> >
> Very slick. Compiler errors for using operator* on a shared_ptr<T[]>
> are a little circuitous, but not too bad. . . though once everyone has
> a compiler-implemented static_assert such errors will be nicer.

I think that it's possible to remove op* completely in C++11 by using a
defaulted template parameter, as in

template<class R = sp_dereference<T>::type> R operator* () const;

with sp_dereference<T[]> not having ::type. But this means even more
#ifdefs, and it's already much too #ifdef-y for my taste, so...

> I assume that, using the aliasing constructor, I'll be able to write
> something like the following:
> shared_ptr<int[]> a1(new int[500]);
> shared_ptr<int[]> a2(a1, &a1[250]);
> a1.reset();//a2 is still okay after this line.

I see no reason for you to not be able to. :-)

> Should shared_ptr<T[]> be constructable from shared_array<T>?

At the moment I'm inclined to leave shared_array as is, purely as a legacy,
and focus on shared_ptr<T[]> as the spiffy new way forward.

> It seems like the following syntax is possible (my toy code seemed to
> work):
> shared_ptr<B> sp1 = make_shared<B>(<B ctor args>);//1
> shared_ptr<B[]> sp2 = make_shared<B[]>(number_of_B);//2: Default
> constructed
> shared_ptr<B[]> sp3 = make_shared<B[]>(number_of_B, <B ctor args>);//3:
> All initialized to this.
> shared_ptr<int[]> sp4 = make_shared<int[]>(number_of_ints);//4:
> uninitialized
> shared_ptr<int[]> sp4 = make_shared<int[]>(number_of_ints, 5);//5: all
> initialized to 5

Glen just proposed the same syntax, and I like it, although I prefer

shared_ptr<int[]> sp4a = make_shared<int[]>(number_of_ints);//4a:
initialized to 0
shared_ptr<int[]> sp4b = make_shared_noinit<int[]>(number_of_ints);//4b:

Boost list run by bdawes at, gregod at, cpdaniel at, john at