Boost logo

Boost :

From: Howard Hinnant (hinnant_at_[hidden])
Date: 2001-01-07 15:05:46


Peter Dimov wrote on 1/7/2001 2:46 PM
>No, this is not what I had in mind. The code checks whether it's dealing
>with the array version. Whether it's called auto_ptr<X[]> or auto_array<X>
>makes no difference.

To form the new expression (single or array) is going to be an issue no
matter what the smart pointer's name is. The checking is done prior to
the construction of the smart pointer in order to form the new
expression. After the construction of the smart pointer, the code is
ignorant of the type of smart pointer (in both examples). (and in the
T[] example, the code is ignorant of the type of smart pointer at the
construction site).

Perhaps my examples should have moved the new expression out of foo and
into main to be more clear:

template <class T>
void
foo(typename remove_bounds<T>::type* p, std::size_t n)
{
        auto_ptr<T> ap(p);
        for (int i = 0; i < n; ++i, ++p)
                do_it(*p); // might throw
}

int main()
{
        foo<int>(new int, 1);
        foo<int[]>(new int[3], 3);
}

-Howard


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