auto_ptr_ref

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Neelesh Bodas

    #1

    auto_ptr_ref

    Hello All,
    My question is regarding the implementation of auto pointers - How
    does the class auto_ptr_ref work?

    eg: suppose I have :

    auto_ptr<int> foo();

    int bar()
    {
    auto_ptr<int*> k = foo();
    }

    The exact sequence of calls as I see here is (no optimizations assumed)
    -
    1) foo returns auto_ptr<int> by value
    2) CC is called on the return value.
    3) The returned auto_ptr<int> is converted to auto_ptr_ref<in t> by
    using operator auto_ptr_ref
    4) Now since this is a copy initialization and not a direct
    initialization, the RHS (whose type is now auto_ptr_ref<in t>) needs to
    get converted to auto_ptr<int> again - done by ctor of auto_ptr<int>
    which takes auto_ptr_ref<in t>
    5) Now CC should be called - but again we are back to old problem - the
    generated auto_ptr is temporary and hence cannot be passed as a
    non-const reference to the function.

    Seems I am going somewhere in cycle :-(

    Josuttis' book doesnot say enough on this -

    <quote>
    "The mechanism relies on a slight difference between the overloading
    and template argument deduction rules. This difference is too subtle to
    be of use as a general programming tool, but it is sufficient to enable
    the auto_ptr class to work correctly."
    </quote>

    Can somebody explain what I am not getting here?

  • Alf P. Steinbach

    #2
    Re: auto_ptr_ref

    * Neelesh Bodas:[color=blue]
    >
    > eg: suppose I have :
    >
    > auto_ptr<int> foo();
    >
    > int bar()
    > {
    > auto_ptr<int*> k = foo();
    > }
    >
    > The exact sequence of calls as I see here is (no optimizations assumed)
    > -
    > 1) foo returns auto_ptr<int> by value[/color]

    OK.

    [color=blue]
    > 2) CC is called on the return value.[/color]

    No, there is no std::auto_ptr( std::auto_ptr const& ) copy constructor.

    [color=blue]
    > 3) The returned auto_ptr<int> is converted to auto_ptr_ref<in t> by
    > using operator auto_ptr_ref[/color]

    OK.

    [color=blue]
    > 4) Now since this is a copy initialization and not a direct
    > initialization, the RHS (whose type is now auto_ptr_ref<in t>) needs to
    > get converted to auto_ptr<int> again - done by ctor of auto_ptr<int>
    > which takes auto_ptr_ref<in t>[/color]

    OK.

    [color=blue]
    > 5) Now CC should be called[/color]

    No, you've reached the goal.

    [color=blue]
    > - but again we are back to old problem - the
    > generated auto_ptr is temporary and hence cannot be passed as a
    > non-const reference to the function.[/color]

    What generated auto_ptr? The one generated is the one you've named 'k'.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?

    Comment

    • Neelesh Bodas

      #3
      Re: auto_ptr_ref

      Hi Alf, Thanks for the help.

      Actually, more I think on this, more confusion it is creating.
      [color=blue][color=green]
      > >
      > > auto_ptr<int> foo();
      > >
      > > int bar()
      > > {
      > > auto_ptr<int*> k = foo();
      > > }[/color][/color]

      Isn't is correct that whever a function returns by value, a CC must get
      called on the return value(Though it might be optimized away by the
      compiler?)
      [color=blue]
      >
      > No, there is no std::auto_ptr( std::auto_ptr const& ) copy constructor.
      >[/color]

      I believe that we do not need a std::auto_ptr( std::auto_ptr const& )
      copy constructor - std::auto_ptr( std::auto_ptr& ) would do the work -
      since the source of the copy inside "foo" is not necessarily a const :

      std::auto_ptr<i nt> foo()
      {
      std::auto_ptr<i nt> v;
      return v; // a copy ctor will be called on v since this is return by
      value
      }

      But when I debugged throgh std::auto_ptr code I didnot find any call to
      the given copy constructor (as you said in the reply.)

      I am not getting where exactly I am going wrong.

      Comment

      • Alf P. Steinbach

        #4
        Re: auto_ptr_ref

        * Neelesh Bodas:[color=blue]
        > Hi Alf, Thanks for the help.
        >
        > Actually, more I think on this, more confusion it is creating.
        >[color=green][color=darkred]
        > > >
        > > > auto_ptr<int> foo();
        > > >
        > > > int bar()
        > > > {
        > > > auto_ptr<int*> k = foo();
        > > > }[/color][/color]
        >
        > Isn't is correct that whever a function returns by value, a CC must get
        > called on the return value(Though it might be optimized away by the
        > compiler?)[/color]

        No. _Some_ constructor must be called. That call can be optimized
        away, but the constructor must be available to be called (the as-if rule
        for optimization).

        [color=blue][color=green]
        > > No, there is no std::auto_ptr( std::auto_ptr const& ) copy constructor.
        > >[/color]
        >
        > I believe that we do not need a std::auto_ptr( std::auto_ptr const& )
        > copy constructor - std::auto_ptr( std::auto_ptr& ) would do the work -
        > since the source of the copy inside "foo" is not necessarily a const :
        >
        > std::auto_ptr<i nt> foo()
        > {
        > std::auto_ptr<i nt> v;
        > return v; // a copy ctor will be called on v since this is return by
        > value
        > }
        >
        > But when I debugged throgh std::auto_ptr code I didnot find any call to
        > the given copy constructor (as you said in the reply.)[/color]

        That is unfortunately a different effect.

        Here the reference-to-non-const copy constructor could be called, and I
        believe _is_ called at the formal level (the C++ abstract machine
        level), but that call is optimized away by your compiler.

        However, declare 'v' as const, or use a temporary, and you're no longer
        in possible copy constructor call territory: _some_ constructor must be
        called (although it can be optimized away), but in the case of const or
        temporary auto_ptr that constructor isn't the copy constructor.

        [color=blue]
        > I am not getting where exactly I am going wrong.[/color]

        This time it was mistaking the compiler's optimization as confirmation
        of a misinterpretati on of what I wrote (sorry about not being more
        clear). I didn't mean that a copy constructor is never called for any
        function result. I meant that a copy constructor does not always have
        to be called, and in particular I meant, and wrote, that std::auto_ptr
        doesn't have a ref-to-const copy constructor, which is the usual copy
        constructor used for a function result.

        --
        A: Because it messes up the order in which people normally read text.
        Q: Why is it such a bad thing?
        A: Top-posting.
        Q: What is the most annoying thing on usenet and in e-mail?

        Comment

        • Neelesh Bodas

          #5
          Re: auto_ptr_ref

          Hello Alf,

          Alf P. Steinbach wrote:
          [color=blue]
          > No. _Some_ constructor must be called. That call can be optimized
          > away, but the constructor must be available to be called (the as-if rule
          > for optimization).[/color]

          Thanks, this clears my misunderstandin g.

          Continuing on my previous mail, we see that there are _two_ user
          defined conversions going on one after the other in a single expression
          -

          1) std::auto_ptr<i nt> to std::auto_ptr_r ef<int> by a call to
          std::auto_ptr<i nt>operator auto_ptr_ref<in t>()
          2) Call to the converting constructor
          std::auto_ptr<i nt>(std::auto_p tr_ref<int>) to create the result of type
          auto_ptr<int>

          If I understand right, since both of these are user defined
          conversions, only one of them is allowed.

          Could you please comment on this?

          Comment

          • Alf P. Steinbach

            #6
            Re: auto_ptr_ref

            * Neelesh Bodas:[color=blue]
            > * Alf P. Steinbach:[color=green]
            > >
            > > No. _Some_ constructor must be called. That call can be optimized
            > > away, but the constructor must be available to be called (the as-if rule
            > > for optimization).[/color]
            >
            > Thanks, this clears my misunderstandin g.
            >
            > Continuing on my previous mail, we see that there are _two_ user
            > defined conversions going on one after the other in a single expression
            > -
            >
            > 1) std::auto_ptr<i nt> to std::auto_ptr_r ef<int> by a call to
            > std::auto_ptr<i nt>operator auto_ptr_ref<in t>()
            > 2) Call to the converting constructor
            > std::auto_ptr<i nt>(std::auto_p tr_ref<int>) to create the result of type
            > auto_ptr<int>
            >
            > If I understand right, since both of these are user defined
            > conversions, only one of them is allowed.
            >
            > Could you please comment on this?[/color]

            I'd rather not... ;-)

            Because it's an issue that people tend to have to agree to disagree on.

            My personal take is that the second doesn't really count as a user
            defined conversion because it's not a conversion producing a temporary;
            it's like plugging a char const* into a std::string constructor with
            direct initialization syntax (see below); no conversion has to be found
            by the compiler, it's a direct match.

            But that is just a convenient shovel-it-under-the-carpet-and-wave-
            yer-hands-frantically explanation, because if you do

            class Base {};
            class Derived: public Base {};

            std::auto_ptr<B ase> x = std::auto_ptr<D erived>( new Derived );

            then you _are_ up against the single conversion rule, and the only thing
            different seems to be use of a templated constructor.

            On the other hand (or is it the third, now), if you write the
            initialization as a direct initialization,

            std::auto_ptr<B ase> x( std::auto_ptr<D erived>( new Derived ) );

            then with a standard-conforming compiler all is a good (note here: MSVC
            7.1 is not a standard-conforming compiler in this respect).

            Perhaps someone who Really Really Knows could be kind enough to comment
            on the formal "why" your point (2) isn't counted in the number of
            user-defined conversions involved -- or perhaps that it is but that
            the standard library is allowed to do anything, whichever is the case.

            --
            A: Because it messes up the order in which people normally read text.
            Q: Why is it such a bad thing?
            A: Top-posting.
            Q: What is the most annoying thing on usenet and in e-mail?

            Comment

            • Neelesh Bodas

              #7
              Re: auto_ptr_ref

              Alf P. Steinbach wrote:
              [color=blue]
              > then you _are_ up against the single conversion rule, and the only thing
              > different seems to be use of a templated constructor.[/color]

              I observed a similar behaviour with a non-templated constructor also -

              class Y { } ;
              class X {
              public:
              X() { }
              X (X&) { } // copy constuctor takes non-const reference
              operator Y();
              X(Y) { }
              } ;

              X foo() { X p; return p; }

              int main()
              {
              X k = foo();
              }

              Here -
              1) foo returns an object of type X by value. This is a temporary.
              2) since this temporary cannot be passed as a reference-to-non-const,
              hence the copy constructor is of no use here
              3) So operator Y() is called on this temporary, and it returns another
              object of type Y.
              4) Now again the converting constructor X(Y) will be called and the
              resulting value is put into k.

              Since this is a copy initialization (and not a direct initialization) ,
              I was thinking that this involved _two_ calls to user defined
              conversion operators. But still this compiles (Comeau online, g++
              3.4.2)

              Comment

              • Aleksey Loginov

                #8
                Re: auto_ptr_ref


                Neelesh Bodas wrote:[color=blue]
                > Hello All,
                > My question is regarding the implementation of auto pointers - How
                > does the class auto_ptr_ref work?
                >
                > eg: suppose I have :
                >
                > auto_ptr<int> foo();
                >
                > int bar()
                > {
                > auto_ptr<int*> k = foo();
                > }
                >[/color]

                as you know, it's not possible to pass RHS "foo()" as non const
                reference "auto_ptr &".
                but you can call it's not const methods "foo().nonconst ()".
                so, "auto_ptr" use that fact to allow constructor modify RHS.

                as illustration
                #include<iostre am>

                struct auto_ptr {
                int n;
                static int next () { static int i; return ++i; }

                struct auto_ptr_ref {
                auto_ptr * ptr;
                auto_ptr_ref ( auto_ptr * p ) : ptr (p) {}
                };

                auto_ptr () : n (next()) { std::cout << this << ":auto_ptr():ne w(" <<
                n << ")" << std::endl; }
                ~auto_ptr () { std::cout << this << ":auto_ptr():de lete(" << n << ")"
                << std::endl; }
                auto_ptr ( auto_ptr & );
                auto_ptr & operator = ( auto_ptr & );

                auto_ptr ( auto_ptr_ref /*by value*/ r ) {
                this->n = r.ptr->n;
                r.ptr->n = 0; // update RHS
                std::cout << this << ":auto_ptr(auto _ptr_ref):" << r.ptr <<
                std::endl;
                }
                operator auto_ptr_ref () {
                std::cout << this << ":operator auto_ptr_ref(): " << std::endl;
                return auto_ptr_ref(th is);
                }

                };

                auto_ptr foo () { auto_ptr ptr; return ptr; }

                int main(){
                auto_ptr ptr = foo ();
                }

                Comment

                • Neelesh Bodas

                  #9
                  Re: auto_ptr_ref

                  Hi Aleksey,
                  thanks for the reply.

                  Aleksey Loginov wrote:
                  [color=blue]
                  > auto_ptr ( auto_ptr_ref /*by value*/ r ) {
                  > this->n = r.ptr->n;
                  > r.ptr->n = 0; // update RHS
                  > std::cout << this << ":auto_ptr(auto _ptr_ref):" << r.ptr <<
                  > std::endl;
                  > }
                  > operator auto_ptr_ref () {
                  > std::cout << this << ":operator auto_ptr_ref(): " << std::endl;
                  > return auto_ptr_ref(th is);
                  > }
                  >
                  > };
                  >
                  > auto_ptr foo () { auto_ptr ptr; return ptr; }
                  >
                  > int main(){
                  > auto_ptr ptr = foo ();
                  > }[/color]

                  The basic reason why I am not able to understand why this works is that
                  I observe _two_ user defined conversions applied here -
                  1) conversion from auto_ptr to auto_ptr_ref using operator auto_ptr_ref
                  () : applied on return value of foo
                  2) conversion from auto_ptr_ref to auto_ptr using the constructor
                  auto_ptr ( auto_ptr_ref) applied on the result of 1.

                  The reason why I am considering (2) as a conversion is that the
                  initialization of ptr is done as a copy initialization - not a direct
                  initialization.

                  Or may be it is the case that (2) above is _not_ considered as a
                  conversion by the compiler somehow. Not sure.

                  Hence the confusion.

                  Comment

                  • mlimber

                    #10
                    Re: auto_ptr_ref

                    Neelesh Bodas wrote:[color=blue]
                    > Hello All,
                    > My question is regarding the implementation of auto pointers - How
                    > does the class auto_ptr_ref work?[/color]
                    [snip]

                    See these links by N. Josuttis and S. Meyers, respectively:

                    The C++ Standard Library auto_ptr, auto_ptr_ref, Nicolai M. Josuttis




                    Cheers! --M

                    Comment

                    • Neelesh Bodas

                      #11
                      Re: auto_ptr_ref

                      Thanks mlimber for the reply.

                      Sorry for going over the same problem again, but it is really leaving
                      me in a confused state. Here is another confusion over the
                      "auto_ptr_r ef" concept :

                      Stroustrup says in TC++PL (14.4.2) :
                      "The purpose of auto_ptr_ref is to implement the destructive copy
                      semantics for ordinary auto_ptrs while making it impossible to copy a
                      const auto_ptr"

                      Josuttis says something similar in "The C++ Std Library" -
                      "Fortunatel y, there was a late design decision that made auto_ptrs less
                      dangerous. By some tricky implementation techniques, transfer of
                      ownership is not possible with constant references. In fact, you can't
                      change the ownership of any constant auto_ptr:"

                      I havenot understood this at all. I understand that use of
                      auto_ptr_ref is to be able return auto_ptrs from functions and use it
                      on RHS or use it as argument to another function. Where does this
                      "Destructiv e copy semantics" and "impossibil ity to copy auto_ptrs" come
                      into picture? (Basically that is already guaranteed when the copy
                      constructor takes non-const reference as argument)

                      Can somebody please enlighten me on this? (It is very well possible
                      that I have missed out some basic points due to excessive thinking :-(
                      ..) I have searched the net all the way, and I have already gone
                      through all the three drafts of auto_ptr implementation (along with the
                      links given by you). But I was unable to find any convincing
                      explanation to these issues.

                      Any reference which discusses design-goals of auto_ptr_ref in details
                      (along with examples - not just a theoretical discussion) will also be
                      useful.

                      Comment

                      • mlimber

                        #12
                        Re: auto_ptr_ref


                        Neelesh Bodas wrote:[color=blue]
                        > Thanks mlimber for the reply.
                        >
                        > Sorry for going over the same problem again, but it is really leaving
                        > me in a confused state. Here is another confusion over the
                        > "auto_ptr_r ef" concept :
                        >
                        > Stroustrup says in TC++PL (14.4.2) :
                        > "The purpose of auto_ptr_ref is to implement the destructive copy
                        > semantics for ordinary auto_ptrs while making it impossible to copy a
                        > const auto_ptr"
                        >
                        > Josuttis says something similar in "The C++ Std Library" -
                        > "Fortunatel y, there was a late design decision that made auto_ptrs less
                        > dangerous. By some tricky implementation techniques, transfer of
                        > ownership is not possible with constant references. In fact, you can't
                        > change the ownership of any constant auto_ptr:"
                        >
                        > I havenot understood this at all. I understand that use of
                        > auto_ptr_ref is to be able return auto_ptrs from functions and use it
                        > on RHS or use it as argument to another function. Where does this
                        > "Destructiv e copy semantics" and "impossibil ity to copy auto_ptrs" come
                        > into picture? (Basically that is already guaranteed when the copy
                        > constructor takes non-const reference as argument)
                        >
                        > Can somebody please enlighten me on this? (It is very well possible
                        > that I have missed out some basic points due to excessive thinking :-(
                        > .) I have searched the net all the way, and I have already gone
                        > through all the three drafts of auto_ptr implementation (along with the
                        > links given by you). But I was unable to find any convincing
                        > explanation to these issues.
                        >
                        > Any reference which discusses design-goals of auto_ptr_ref in details
                        > (along with examples - not just a theoretical discussion) will also be
                        > useful.[/color]

                        Destructive copy semantics means that passing an auto_ptr around
                        transfers ownership, and that is precisely what distinguishes it from
                        boost::scoped_p tr. To achieve this design goal, Bill Gibbons and Greg
                        Colvin proposed the "tricky implementation" of using conversions to
                        auto_ptr_ref detailed at the end of the second link. Please see their
                        discussion in the section "Analysis of Conversion operations". If you
                        have specific questions about it, feel free to ask.

                        Cheers! --M

                        Comment

                        • Aleksey Loginov

                          #13
                          Re: auto_ptr_ref


                          Neelesh Bodas wrote:[color=blue]
                          > Hi Aleksey,
                          > thanks for the reply.
                          >
                          > Aleksey Loginov wrote:
                          >[color=green]
                          > > auto_ptr ( auto_ptr_ref /*by value*/ r ) {
                          > > this->n = r.ptr->n;
                          > > r.ptr->n = 0; // update RHS
                          > > std::cout << this << ":auto_ptr(auto _ptr_ref):" << r.ptr <<
                          > > std::endl;
                          > > }
                          > > operator auto_ptr_ref () {
                          > > std::cout << this << ":operator auto_ptr_ref(): " << std::endl;
                          > > return auto_ptr_ref(th is);
                          > > }
                          > >
                          > > };
                          > >
                          > > auto_ptr foo () { auto_ptr ptr; return ptr; }
                          > >
                          > > int main(){
                          > > auto_ptr ptr = foo ();
                          > > }[/color]
                          >
                          > The basic reason why I am not able to understand why this works is that
                          > I observe _two_ user defined conversions applied here -
                          > 1) conversion from auto_ptr to auto_ptr_ref using operator auto_ptr_ref
                          > () : applied on return value of foo
                          > 2) conversion from auto_ptr_ref to auto_ptr using the constructor
                          > auto_ptr ( auto_ptr_ref) applied on the result of 1.
                          >[/color]

                          I try to explain.
                          When foo returns ptr, compiler may call "auto_ptr ( auto_ptr & )" (1
                          step)
                          or "auto_ptr ( auto_ptr_ref & )" through "operator auto_ptr_ref ()" (2
                          steps).
                          First case preferred, because less steps must be done. (see Andrey's
                          post
                          http://groups.google.com/group/comp....4a47959afdddf7)
                          But in real, compiler allowed (and it is) not to do all that calls, and
                          return ptr as RHS.
                          [color=blue]
                          > The reason why I am considering (2) as a conversion is that the
                          > initialization of ptr is done as a copy initialization - not a direct
                          > initialization.
                          >
                          > Or may be it is the case that (2) above is _not_ considered as a
                          > conversion by the compiler somehow. Not sure.
                          >
                          > Hence the confusion.[/color]

                          Hope this help. Sorry for dummy language.

                          Comment

                          • Neelesh Bodas

                            #14
                            Re: auto_ptr_ref

                            Hello mlimber,
                            [color=blue]
                            > Please see their discussion in the section "Analysis of Conversion operations". If you
                            > have specific questions about it, feel free to ask.[/color]

                            Thanks a lot for this. I read the link again, and it seems that my all
                            doubts have finally been answered satisfactorily.
                            So just thought of answering them here for others also. (please do
                            correct if my explanation is not appropriate)
                            [color=blue]
                            > Neelesh Bodas wrote:
                            > The basic reason why I am not able to understand why this works is that
                            > I observe _two_ user defined conversions applied here -
                            > 1) conversion from auto_ptr to auto_ptr_ref using operator auto_ptr_ref
                            > () : applied on return value of foo
                            > 2) conversion from auto_ptr_ref to auto_ptr using the constructor
                            > auto_ptr ( auto_ptr_ref) applied on the result of 1.[/color]

                            (2) is _not_ a conversion in this case. Quoting 8.5p14 -

                            "If the initialization is direct initialization, or if it is
                            copy-initialization where the cv-unqualified version of the source type
                            is the same class as, or a derived class of, the class of the
                            destination, constructors are considered."

                            Hence in the current case, when we say
                            std::auto_ptr<i nt> k = foo();
                            the source type ( = return type of foo() ) is std::auto_ptr<i nt> and
                            the destination type is also the same. Hence an constructor will be
                            considered. The only suitable constructor is
                            std::auto_ptr:: auto_ptr(std::a uto_ptr_ref).
                            This constructor can be called by doing a conversion from std::auto_ptr
                            to std::auto_ptr_r ef using std::auto_ptr:: operator auto_ptr_ref()
                            Hence there is only one user defined conversion involved, and the
                            constructor is called on the temporary that is the result of the
                            conversion.
                            [color=blue]
                            >Neelesh Bodas wrote:
                            > 4) Now since this is a copy initialization and not a direct
                            > initialization, the RHS (whose type is now auto_ptr_ref<in t>) needs to
                            > get converted to auto_ptr<int> again - done by ctor of auto_ptr<int>
                            > which takes auto_ptr_ref<in t>[/color]

                            No, that is not correct.Note that the copy initialization has source
                            and destination types (cv -unqualified) same - and this is treated as a
                            special case, and it is considered differently (8.5p14), and an
                            initialization is possible without a call to a copy constructor.
                            [color=blue]
                            > 5) Now CC should be called - but again we are back to old problem -[/color]

                            So no unnecessary calls to copy constructor are made in this case.
                            [color=blue]
                            > Neelesh Bodas wrote:
                            > I observed a similar behavior with a non-templated constructor also -
                            > int main()
                            > {
                            > X k = foo();
                            > }[/color]

                            The same story here.

                            Thanks a lot to Alf, mlimber and Aleksey for their invaluable help and
                            insights in the problem.

                            Comment

                            • Howard Hinnant

                              #15
                              Re: auto_ptr_ref

                              In article <43830983.10632 89546@news.indi vidual.net>,
                              alfps@start.no (Alf P. Steinbach) wrote:
                              [color=blue]
                              > Perhaps someone who Really Really Knows could be kind enough to comment
                              > on the formal "why" your point (2) isn't counted in the number of
                              > user-defined conversions involved -- or perhaps that it is but that
                              > the standard library is allowed to do anything, whichever is the case.[/color]

                              I'm just catching up on my news reading and read through this thread
                              with some amusement. And I won't pretend to be able to follow all of
                              the twists and turns of the standard as the three of you have just done,
                              though I have studied it intently in the past (and perhaps even
                              understood it for awhile).

                              My reason for posting is just to point out that auto_ptr has been a very
                              valuable (but failed) experiment. auto_ptr has been the pioneer of
                              "move semantics". It is my hope that a much simpler form of move
                              semantics will appear in C++0X, including a (yet again) redesigned
                              auto_ptr under the name of unique_ptr. The new unique_ptr will do
                              everything auto_ptr does now (without auto_ptr_ref tricks) except one
                              thing: It will not move from an lvalue:

                              unique_ptr<int> p1;
                              unique_ptr<int> p2 = p1; // compile time error

                              However it will move from an rvalue:

                              unique_ptr<int> foo();

                              int bar()
                              {
                              unique_ptr<int> k = foo(); // ok
                              }

                              For more details see:


                              %20-%20Class%20temp late%20auto_ptr

                              (watch the wrap)

                              The most exciting thing about this (imho) is that unique_ptr is so
                              simple that you will easily be able to mimic its behavior in your own
                              classes (presumably the reason for devoting such study to auto_ptr). In
                              a nutshell, this is the design of unique_ptr:

                              template <class T>
                              class unique_ptr
                              {
                              public:
                              ...
                              unique_ptr(uniq ue_ptr&& u); // rvalues bind here
                              ...
                              private:
                              unique_ptr(cons t unique_ptr&); // lvalues bind here
                              };

                              Once you get past the rvalue reference (&&), things just get really
                              simple.

                              -Howard

                              Comment

                              Working...