Boost logo

Boost :

Subject: Re: [boost] make_shared_array
From: Nathan Crookston (nathan.crookston_at_[hidden])
Date: 2012-11-02 20:47:30


On Fri, Nov 2, 2012 at 6:20 PM, Peter Dimov <lists_at_[hidden]> wrote:
> Nathan Crookston wrote:
>>
>> On Fri, Nov 2, 2012 at 11:47 AM, Peter Dimov <lists_at_[hidden]> wrote:
>> >
>> > Voila:
>> >
>> > https://svn.boost.org/trac/boost/changeset/81149
>>
>> 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.
Fair enough -- I'll be planning to convert my usage to shared_ptr<T[]>
when it makes it to a release.

>> 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:
> uninitialized

To be clear, is there still a version that would initialize the array
to a particular value?

While I'd prefer to only initialize if an initializer is given (like
std::vector<int>), so long as some incantation allows me to opt-out
then I'll be fine.

To explain my most common use case: I personally use such arrays when
dealing with imagery -- I usually allocate the array, then copy the
data in from some external source. So, I'll generally call the
uninitialized version. I understand other people's use cases differ.

Thanks,
Nate


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