NRV question

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

    #1

    NRV question

    A sample in <inside c++ object model>:

    X bar()
    {
    X xx;
    //...
    return xx;
    }

    void bar(X &__result)
    {
    __result.X::X() ;
    //...
    return;
    }

    Why X::X() is called inside bar? I think it should be called before bar, for
    better semantic.To always reset a input parameter (by calling its
    constructor here) is senseless.

    Is the latter better than the former anyway?
    X x0=bar();
    1. X::X() is called inside bar
    X x0;
    bar(x0);
    2. outside
    X x0;
    x0.X::X();
    bar(x0);


  • Pete Becker

    #2
    Re: NRV question

    jronald wrote:
    >
    Why X::X() is called inside bar? I think it should be called before bar, for
    better semantic.To always reset a input parameter (by calling its
    constructor here) is senseless.
    >
    The compiler doesn't know at the point where it calls bar() how bar is
    going to create the returned object. Suppose it does this:

    X bar()
    {
    X xx(3);
    return xx;
    }

    Now default constructing the return object at the point of the call is
    simply wrong.

    --

    -- Pete

    Author of "The Standard C++ Library Extensions: a Tutorial and
    Reference." For more information about this book, see
    www.petebecker.com/tr1book.

    Comment

    • jronald

      #3
      Re: NRV question

      I was a little confused just now. You are right.
      It's the function generated the object.
      The complier treats "X x0;" a memory allocation only, which is different
      from C++ semantic. In C++ semantic, "X x0" imply a call to the default
      constructor.

      "Pete Becker" <petebecker@acm .org>
      ??????:wYudnbP-7MXgkpHYnZ2dnUV Z_tOdnZ2d@gigan ews.com...
      jronald wrote:
      >>
      >Why X::X() is called inside bar? I think it should be called before bar,
      >for better semantic.To always reset a input parameter (by calling its
      >constructor here) is senseless.
      >>
      >
      The compiler doesn't know at the point where it calls bar() how bar is
      going to create the returned object. Suppose it does this:
      >
      X bar()
      {
      X xx(3);
      return xx;
      }
      >
      Now default constructing the return object at the point of the call is
      simply wrong.
      >
      --
      >
      -- Pete
      >
      Author of "The Standard C++ Library Extensions: a Tutorial and Reference."
      For more information about this book, see www.petebecker.com/tr1book.

      Comment

      • Pete Becker

        #4
        Re: NRV question

        jronald wrote:
        It's the function generated the object.
        The complier treats "X x0;" a memory allocation only, which is different
        from C++ semantic. In C++ semantic, "X x0" imply a call to the default
        constructor.
        Right. You have to be extra careful in discussions about what's going on
        behind the scenes, because the compiler can do things that are different
        from what the code suggests, provided the behavior of the program is the
        same.

        --

        -- Pete

        Author of "The Standard C++ Library Extensions: a Tutorial and
        Reference." For more information about this book, see
        www.petebecker.com/tr1book.

        Comment

        Working...