Quote:Original post by SiCrane
In order to implement return from functions, auto_ptr<> uses a member type, auto_ptr_ref<>. You'll need to implement a constructor that takes an auto_array_ref<> and a conversion operator to auto_array_ref<>. See your auto_ptr implementation for details of what that involves.
Ok, thanks!
...
Quote:bubu LV
You could use boost::shader_array for that.
Quote:Bjarne Stroustrup
So should we use an auto_array to hold arrays? No. There is no auto_array. The reason is that there isn't a need for one. A better solution is to use a vector:
I disagree.
First of all, auto_array gives a documentation advantage. A shared_array doesn't prohibit sharing, whereas auto_array simply says: this will only have one owner at the time.
Additionally, it allows for writing of more general and reusable library code. Example:
std::auto_array<Foo> foo();
This methods needs no knowledge whatsoever about what smart_ptr the user will put the returned array in. Thus, std::auto_array<Foo> foo() is both clean, generic and exception safe. Compare with:
Foo *foo();
This requires more effort to get exception safe, because the "Foo *" may be allocated some place inside the method, the data inside the array modified for a while, and the array finally returned. Or, the returned value may be sent directly as actual parameter to a constructor, only that an exception is thrown when some earlier member in the initialization list of that constructor is initialized. Another alternative:
boost::shared_array<Foo> foo();
This is clean and exception safe, but not general. Now all users must use a shared_array. They can't choose to assign the object to a boost::scoped_array, for example, or any other memory management class of their choice. Another alternative:
std::vector<Foo> foo();
Is again not generic. We are unable to later decide to use the allocated memory as a shared_array, unless we wrap yet another shared_ptr on top of it. But then, we not only get unnecessary overhead for scoped_array cases, but we also force all our client objects to change their interfaces to use shared instead of scoped, etc.
In short:
without an std::auto_array, there's no way to make such a method simultaneously clean, exception safe and general in terms of what memory management strategy the client can choose to give to the returned pointer.