auto_ptr and sink function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marcin Vorbrodt

    auto_ptr and sink function

    A source function is a one that returns an auto_ptr object. A sink function
    is one that takes auto_ptr as a parameter. So:

    void Source(auto_ptr <SomeClass> aptr);

    This will transfer the ownership correctly. How about this:

    void Source(auto_ptr <SomeClass> & aptr);

    Passing it as reference (non-const) should still do the trick, right?
    Is there an advantage to doing so? Speed-wise I mean?

    Martin


  • David B. Held

    #2
    Re: auto_ptr and sink function

    "Marcin Vorbrodt" <mvorbro@eos.nc su.edu> wrote in message
    news:bklgad$qir $1@uni00nw.unit y.ncsu.edu...[color=blue]
    > [...]
    > This will transfer the ownership correctly. How about this:
    >
    > void Source(auto_ptr <SomeClass> & aptr);
    >
    > Passing it as reference (non-const) should still do the trick,
    > right? Is there an advantage to doing so? Speed-wise I
    > mean?[/color]

    For suitable definitions of "trick", sure. But note that it
    doesn't call the "move c'tor", so ownership is not transferred.

    Dave


    Comment

    • Marcin Vorbrodt

      #3
      Re: auto_ptr and sink function

      what if that reference is then assigned to auto-pointer like this:

      class C {
      public:
      C(auto_ptr<X> & ptr) : _ptr(ptr) {}
      private:
      auto_ptr<X> _ptr;
      };

      Will that transfer the ownership correctly?

      "David B. Held" <dheld@codelogi cconsulting.com > wrote in message
      news:bkllf8$iom $1@news.astound .net...[color=blue]
      > "Marcin Vorbrodt" <mvorbro@eos.nc su.edu> wrote in message
      > news:bklgad$qir $1@uni00nw.unit y.ncsu.edu...[color=green]
      > > [...]
      > > This will transfer the ownership correctly. How about this:
      > >
      > > void Source(auto_ptr <SomeClass> & aptr);
      > >
      > > Passing it as reference (non-const) should still do the trick,
      > > right? Is there an advantage to doing so? Speed-wise I
      > > mean?[/color]
      >
      > For suitable definitions of "trick", sure. But note that it
      > doesn't call the "move c'tor", so ownership is not transferred.
      >
      > Dave
      >
      >[/color]


      Comment

      • David B. Held

        #4
        Re: auto_ptr and sink function

        "Marcin Vorbrodt" <mvorbro@eos.nc su.edu> wrote in message
        news:bklm32$sub $1@uni00nw.unit y.ncsu.edu...[color=blue]
        > what if that reference is then assigned to auto-pointer like this:
        >
        > class C {
        > public:
        > C(auto_ptr<X> & ptr) : _ptr(ptr) {}
        > private:
        > auto_ptr<X> _ptr;
        > };
        >
        > Will that transfer the ownership correctly?[/color]

        Since you now have a different instance of auto_ptr<X>,
        yes, the copy [move] c'tor will be called and ownership
        transferred.

        Dave


        Comment

        • tom_usenet

          #5
          Re: auto_ptr and sink function

          On Sun, 21 Sep 2003 20:39:06 -0400, "Marcin Vorbrodt"
          <mvorbro@eos.nc su.edu> wrote:
          [color=blue]
          >A source function is a one that returns an auto_ptr object. A sink function
          >is one that takes auto_ptr as a parameter. So:
          >
          >void Source(auto_ptr <SomeClass> aptr);
          >
          >This will transfer the ownership correctly. How about this:
          >
          >void Source(auto_ptr <SomeClass> & aptr);
          >
          >Passing it as reference (non-const) should still do the trick, right?
          >Is there an advantage to doing so? Speed-wise I mean?[/color]

          It is probably marginally faster (depending on optimization settings),
          but it is no longer explicit. e.g.

          void Sink(auto_ptr<S omeClass> & aptr)
          {
          aptr.reset(new SomeClass);
          //actually a source!
          }

          This obviously isn't possible if you pass by value, so the parameter
          is a proper sink. In addition, you can pass temporary auto_ptrs by
          value. e.g.

          //illegal for reference version:
          Sink(auto_ptr<S omeClass>(new SomeClass));

          Tom

          Comment

          Working...