reference to return value

This is a discussion on reference to return value within the C++ Programming forums, part of the General Programming Boards category; Is the following code safe? Code: GroupSet GetSettings() { GroupSet g; /*fill g*/ return g; } GroupSet &refSet = Gettings(); ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    330

    reference to return value

    Is the following code safe?

    Code:
    GroupSet GetSettings() { GroupSet g; /*fill g*/ return g; }
    
    GroupSet &refSet = Gettings();
    I would say not since a reference to a temporary object is kept. Or is there RVO in action here so it might break on another compiler?

  2. #2
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    No, not safe because a reference to a temporary is being kept, like you said. The return value of GetSettings() goes out of scope after a reference to it is stored in refSet. Then again, if you never use refSet you won't have a problem. RVO is irrelevant here, but the behaviour is undefined if you go and use refSet, so you may see differences for different compilers/different systems/even different executions.

    On second thought, have you tested this code? I got a compiler error with this. There's actually a weird rule about non-const references surrounding this issue:

    Code:
    #include <iostream>
    
    int GetInt() { return 3; }
    
    int main()
    {
        int& x = GetInt();
        std::cout << x << std::endl;
    
        return 0;
    }
    Code:
    tempRef.cpp: In function ‘int main()’:
    tempRef.cpp:7: error: invalid initialization of non-const reference of type
    ‘int&’ from a temporary of type ‘int’
    Last edited by Mozza314; 03-31-2011 at 04:41 AM.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Visual C++ will accept this, but it's not correct standard C++.

    If you change the reference to be const, it's safe, but only as long as you follow the rules. People often make mistakes there, so it might be better to avoid it unless you know what you're doing.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    Thanks a lot. We use VC yes so it works. Too much pressure right now to change it everywhere but noted for if we ever change compilers

  5. #5
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    Whether it's a const or non-const, you still have the problem that you're referencing something which doesn't exist anymore, so the program is undefined and you can get a segfault. For this reason, I really wouldn't want to just leave it as is. Even though it may seem to work now, it could blow up later without you knowing what it is and that would really be time consuming.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,980
    Quote Originally Posted by Mozza314
    Whether it's a const or non-const, you still have the problem that you're referencing something which doesn't exist anymore, so the program is undefined and you can get a segfault.
    Not quite: CornedBee is right to say that "if you change the reference to be const, it's safe, but only as long as you follow the rules" because the lifetime of the object returned would be extended to that of the const reference.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    Quote Originally Posted by laserlight View Post
    Not quite: CornedBee is right to say that "if you change the reference to be const, it's safe, but only as long as you follow the rules" because the lifetime of the object returned would be extended to that of the const reference.
    Bizarre! Thanks; I didn't know that one. Is there a name for it or do you know a good place I could read more about it?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An efficient approach to TicTacToe AI?
    By xofvc4rqb in forum C++ Programming
    Replies: 11
    Last Post: 01-15-2011, 04:09 PM
  2. Can you help me about tolower() in file
    By nctar in forum C Programming
    Replies: 7
    Last Post: 05-12-2010, 10:04 AM
  3. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  4. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  5. Alegro closes out on me
    By campsoup1988 in forum C++ Programming
    Replies: 8
    Last Post: 04-03-2006, 10:40 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21