good code to return const reference to function local object?

Discussion in 'C++' started by George2, Jan 23, 2008.

  1. George2

    George2 Guest

    Hello everyone,


    1. Returning non-const reference to function local object is not
    correct. But is it correct to return const reference to function local
    object?

    2. If in (1), it is correct to return const reference to function
    local object, the process is a new temporary object is created (based
    on the function local object) and the const reference is binded to the
    temporary object, and the life time of the temporary object is
    extended to the life time of const reference?

    Or no need to create such a temporary object, just let the const
    reference binded to the function local object itself?


    thanks in advance,
    George
     
    George2, Jan 23, 2008
    #1
    1. Advertising

  2. George2

    James Kanze Guest

    On Jan 23, 12:26 pm, George2 <> wrote:

    > 1. Returning non-const reference to function local object is not
    > correct. But is it correct to return const reference to function local
    > object?


    No. What would that change?

    > 2. If in (1), it is correct to return const reference to function
    > local object, the process is a new temporary object is created (based
    > on the function local object) and the const reference is binded to the
    > temporary object, and the life time of the temporary object is
    > extended to the life time of const reference?


    No. To begin with, a temporary is never created when a
    reference is initialized with an lvalue. And even if it were,
    "A temporary bound to the [reference type] return value of a
    function persists until the function exits".

    > Or no need to create such a temporary object, just let the
    > const reference binded to the function local object itself?


    Const or not, the reference binds to the function local object,
    which is destructed when the function exits.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Jan 24, 2008
    #2
    1. Advertising

  3. George2

    Guest

    According to http://herbsutter.spaces.live.com/blog/cns!2D4327CC297151BB!378..entry
    You CAN return a const reference to a function local object and it IS
    kept alive
    as long as the reference lives.

    A copy is NOT created, you are passing a const reference to the actual
    temporary in the
    function.



    On Jan 24, 12:12 am, James Kanze <> wrote:
    > On Jan 23, 12:26 pm, George2 <> wrote:
    >
    > > 1. Returning non-const reference to function local object is not
    > > correct. But is it correct to return const reference to function local
    > > object?

    >
    > No. What would that change?
    >
    > > 2. If in (1), it is correct to return const reference to function
    > > local object, the process is a new temporary object is created (based
    > > on the function local object) and the const reference is binded to the
    > > temporary object, and the life time of the temporary object is
    > > extended to the life time of const reference?

    >
    > No. To begin with, a temporary is never created when a
    > reference is initialized with an lvalue. And even if it were,
    > "A temporary bound to the [reference type] return value of a
    > function persists until the function exits".
    >
    > > Or no need to create such a temporary object, just let the
    > > const reference binded to the function local object itself?

    >
    > Const or not, the reference binds to the function local object,
    > which is destructed when the function exits.
    >
    > --
    > James Kanze (GABI Software) email:
    > Conseils en informatique orientée objet/
    > Beratung in objektorientierter Datenverarbeitung
    > 9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    , Jan 24, 2008
    #3
  4. * :
    > According to http://herbsutter.spaces.live.com/blog/cns!2D4327CC297151BB!378.entry
    > You CAN return a const reference to a function local object and it IS
    > kept alive
    > as long as the reference lives.
    >
    > A copy is NOT created, you are passing a const reference to the actual
    > temporary in the
    > function.


    Don't top-post.

    Your interpretation of that article is incorrect.


    Cheers, & hth.,

    - Alf


    --
    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?
     
    Alf P. Steinbach, Jan 24, 2008
    #4
  5. George2

    Kira Yamato Guest

    On 2008-01-24 12:48:23 -0500, said:

    > According to http://herbsutter.spaces.live.com/blog/cns!2D4327CC297151BB!378
    > .entry
    > You CAN return a const reference to a function local object and it IS
    > kept alive
    > as long as the reference lives.


    You're confusing C++ with java.

    >
    > [...]


    --

    -kira
     
    Kira Yamato, Jan 24, 2008
    #5
  6. George2

    James Kanze Guest

    On Jan 24, 6:48 pm, wrote:
    > According
    > tohttp://herbsutter.spaces.live.com/blog/cns!2D4327CC297151BB!378.entry
    > You CAN return a const reference to a function local object
    > and it IS kept alive as long as the reference lives.


    According to the standard, you cannot. And I'd suggest you read
    the article you site; there is no mention of any reference type
    return values at all in it.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Jan 25, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. klaus triendl
    Replies:
    5
    Views:
    5,152
    klaus triendl
    Jun 4, 2004
  2. Jim Langston
    Replies:
    2
    Views:
    473
    Rolf Magnus
    May 11, 2006
  3. Javier
    Replies:
    2
    Views:
    621
    James Kanze
    Sep 4, 2007
  4. George2
    Replies:
    15
    Views:
    718
    James Kanze
    Dec 17, 2007
  5. Arv
    Replies:
    15
    Views:
    952
    James Kanze
    Mar 7, 2008
Loading...

Share This Page