Boost logo

Boost :

From: Howard Hinnant (howard.hinnant_at_[hidden])
Date: 2006-03-14 09:25:36


On Mar 14, 2006, at 2:04 AM, Ralf W. Grosse-Kunstleve wrote:

> --- Howard Hinnant <howard.hinnant_at_[hidden]> wrote:
>
>> According to this proposal your code would be:
>>
>> std::unique_ptr<int[]>
>> foo(unsigned size)
>> {
>> std::unique_ptr<int[]> result(new int[size]);
>> // do something to result
>> // if an exception is thrown result will be cleaned up
>> return result;
>> }
>
> Hi Howard, I fetched your emulation code and tried to compile the
> function
> above; the only change I made was to put your entire file into my
> own namespace
> (instead of std::). Both EDG 245 and gcc 3.4 give similar errors:
>
> cxx: Error: /net/legless/scratch1/rwgk/dist/iotbx/include/iotbx/pdb/
> input.h,
> line 1035: #330-D
> "scitbx::unique_ptr<T [], D>::unique_ptr
> (scitbx::unique_ptr<T [], D>
> &) [with T=int, D=scitbx::default_delete<int []>]" is
> inaccessible
> return result;
> -----------^
>
> Is this an oversight, or do I have to use unique_ptr<> somehow
> differently?

Hi Ralf,

This is one of the areas where the emulated version is a little
different from the real deal. You can work around this in one of two
ways:

    unique_ptr<int[]>
    foo(unsigned size)
    {
      unique_ptr<int[]> result(new int[size]);
      // do something to result
      // if an exception is thrown result will be cleaned up
      return move(result);
    }

or:

    unique_ptr<int[]>
    foo(unsigned size)
    {
      return unique_ptr<int[]>(new int[size]);
    }

The move() effectively turns result into an rvalue which can then be
moved from. In the move proposal the C++ language is changed such
that expressions subject to RVO are implicitly treated as rvalues.

Hope this helps.

-Howard


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