Loading ...
Sorry, an error occurred while loading the content.

interesting C++ article

Expand Messages
  • Steve Dekorte
    http://www.self-similar.com/temporaries.html Whenever a return statement is executed in a C++ program, an object is copied to a location, and then
    Message 1 of 4 , Jun 1, 2003
    View Source
    • 0 Attachment
      http://www.self-similar.com/temporaries.html

      "Whenever a "return" statement is executed in a C++ program, an object
      is copied to a location, and then immediately destroyed, twice, once
      inside and once outside the function. This object is actually
      being moved from one memory location to another. In C++, however, each
      copy is implemented with two function calls: one to copy-constructor,
      and one to destructor. I show how to avoid these expensive and
      unnecessary calls."

      Cheers,
      Steve
      OSX freeware and shareware: http://www.dekorte.com/downloads.html
    • Mike
      From an article written by Stanley B. Lippman: ... It is called the name return value optimization. It is applied implicitly by a compiler when the same named
      Message 2 of 4 , Jun 2, 2003
      View Source
      • 0 Attachment
        From an article written by Stanley B. Lippman:

        "... It is called the name return value optimization. It is applied
        implicitly by a compiler when the same named object is returned from
        all the exit points of a function. ..."

        http://www.informit.com/isapi/product_id~%7BCDBE41DA-88BF-42E9-959F-
        27CB4F5DFAE1%7D/element_id~%7B664D09FE-2EE0-4343-B744-F0E1E407E50B%
        7D/st~%7B3FAD3499-20A6-4782-9A96-05825F8E6E5B%7D/content/articlex.asp

        In the following code, the error variable should be optimized away:

        struct Error {
        char* message;
        int code;
        };

        Error compile() {
        Error error;
        error.message = "Out of mem";
        error.code = 10;
        return error;
        }

        The above should be equivalent the following if your C++ compiler
        implements the return value optimization, which I guess my gcc 2.95.3-
        5 does NOT. :(

        Error& compile( Error& error ) {
        error.message = "Out of mem";
        error.code = 10;
        return error;
        }

        Mike

        --- In langsmiths@yahoogroups.com, Steve Dekorte <steve@d...> wrote:
        >
        > http://www.self-similar.com/temporaries.html
        >
        > "Whenever a "return" statement is executed in a C++ program, an
        object
        > is copied to a location, and then immediately destroyed, twice,
        once
        > inside and once outside the function. This object is
        actually
        > being moved from one memory location to another. In C++, however,
        each
        > copy is implemented with two function calls: one to copy-
        constructor,
        > and one to destructor. I show how to avoid these expensive and
        > unnecessary calls."
        >
        > Cheers,
        > Steve
        > OSX freeware and shareware: http://www.dekorte.com/downloads.html
      • Chris Double
        ... Does it work if you don t give the object a name? Like: struct Error { char* message; int code; Error(char* m, int c) : message(m), code(c) {} }; Error
        Message 3 of 4 , Jun 2, 2003
        View Source
        • 0 Attachment
          On Mon, 02 Jun 2003 19:12:13 -0000, "Mike" <mike_ekim@...> said:
          > The above should be equivalent the following if your C++ compiler
          > implements the return value optimization, which I guess my gcc 2.95.3-
          > 5 does NOT. :(

          Does it work if you don't give the object a name? Like:

          struct Error {
          char* message;
          int code;

          Error(char* m, int c) : message(m), code(c) {}
          };

          Error compile() {
          return Error("Out of mem", 10);
          }

          I seem to recall some compilers optimizing the copy away only if it is an
          unamed object.

          Chris.
          --
          Chris Double
          chris.double@...
        • Mike
          I am now using gcc 3.2, and all of the following functions invoke the constructor only once: foobar f1( int val ) { ` foobar local(1); ` local.ival( val ); `
          Message 4 of 4 , Jun 2, 2003
          View Source
          • 0 Attachment
            I am now using gcc 3.2, and all of the following functions invoke the
            constructor only once:

            foobar f1( int val ) {
            ` foobar local(1);
            ` local.ival( val );
            ` return local;
            }

            foobar f2( int val ) {
            ` return foobar(1);
            }

            foobar f3( int val ) {
            ` if( val < 1000 )
            ` ` return foobar(1);
            ` else
            ` ` return foobar(2);
            }

            So this is good :)
            Mike

            --- In langsmiths@yahoogroups.com, "Chris Double" <chris.double@d...>
            wrote:
            > On Mon, 02 Jun 2003 19:12:13 -0000, "Mike" <mike_ekim@y...> said:
            > > The above should be equivalent the following if your C++ compiler
            > > implements the return value optimization, which I guess my gcc
            2.95.3-
            > > 5 does NOT. :(
            >
            > Does it work if you don't give the object a name? Like:
            >
            > struct Error {
            > char* message;
            > int code;
            >
            > Error(char* m, int c) : message(m), code(c) {}
            > };
            >
            > Error compile() {
            > return Error("Out of mem", 10);
            > }
            >
            > I seem to recall some compilers optimizing the copy away only if it
            is an
            > unamed object.
            >
            > Chris.
            > --
            > Chris Double
            > chris.double@d...
          Your message has been successfully submitted and would be delivered to recipients shortly.