Boost logo

Boost :

From: Paul Mensonides (pmenso57_at_[hidden])
Date: 2003-02-04 21:25:11


Andrei Alexandrescu wrote:

>> I agree that vector (or vector-like substitutes) are preferred over
>> smart_ptr<T[]> in most circumstances. But there are times when
>> ownership of the array needs to be transferred or shared. In those
>> instances, vector is not a good fit. smart_ptr<vector<T> > is a
>> possibility, but this is typically inferior both in performance, and
>> in client level syntax (e.g. (*p)[i] instead of p[i]).
>
> When designing an amenity, it is important to figure out how the
> alternatives work for its target audience. For the limited audience
> that (1) needs a smart pointer to an array and (2) is unhappy with
> vector's performance, I'd say it's ok to just advice using a SmartPtr
> configured
> with an array deleter and have them write get_impl(sp)[45] instead of
> sp[45]. The super-performance-critical code portions of an app are
> not that large and much uglier for different reasons anyway :o).

If an implicit conversion to the pointed-to type is provided, there is no need
to overload the subscript operator:

#include <iostream>

struct sample {
    inline operator const char*() const {
        return "01234";
    }
};

int main() {
    sample var;
    std::cout << var[0] << var[1] << &std::endl;
    return 0;
}

The same applies to the standing problem of operator->*().

2c.

Paul Mensonides


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