Is returning a reference to a constant undefined?

Discussion in 'C++' started by Old Admiral, Mar 14, 2005.

  1. Old Admiral

    Old Admiral Guest

    Gentlemen:


    Is this UB?


    #include <iostream>

    const int& f()
    {
    return 3;
    }

    int main()
    {
    const int& s = f();
    std::cout << s << '\n';
    }


    My compiler does warn me about it, but what I would like to know is
    this:

    Wouldn't the "const int&" part bind to the temporary like in the case
    of:

    const int& i = 3;

    ?

    Thanks for your help.


    Old Admiral salutes you.
    OA.
    Old Admiral, Mar 14, 2005
    #1
    1. Advertising

  2. Old Admiral wrote:

    > Gentlemen:
    >
    >
    > Is this UB?
    >
    >
    > #include <iostream>
    >
    > const int& f()
    > {
    > return 3;
    > }
    >
    > int main()
    > {
    > const int& s = f();
    > std::cout << s << '\n';
    > }
    >



    It is undefined behaviour because you *return* a reference to a local
    object.



    > My compiler does warn me about it, but what I would like to know is
    > this:
    >
    > Wouldn't the "const int&" part bind to the temporary like in the case
    > of:
    >
    > const int& i = 3;



    No they are not the same. But by only changing the function signature to:


    const int f()


    we can say that this is the same and valid.



    --
    Ioannis Vranos

    http://www23.brinkster.com/noicys
    Ioannis Vranos, Mar 14, 2005
    #2
    1. Advertising

  3. Old Admiral wrote:
    > Gentlemen:
    >
    >
    > Is this UB?


    Yes, you're returning a reference to a local literal.

    > #include <iostream>
    >
    > const int& f()
    > {
    > return 3;
    > }
    >
    > int main()
    > {
    > const int& s = f();
    > std::cout << s << '\n';
    > }
    >
    >
    > My compiler does warn me about it, but what I would like to know is
    > this:
    >
    > Wouldn't the "const int&" part bind to the temporary like in the case
    > of:
    >
    > const int& i = 3;
    >
    > ?


    No. In the case of

    const int & i = 3;

    both 3 and 'i' have the same scope. In your case, 3 disappears as soon
    as the function where it exists finishes.

    V
    Victor Bazarov, Mar 14, 2005
    #3
  4. Old Admiral

    Old Admiral Guest

    Bazarov, Vranos:

    Thanks for your help.


    OA
    Old Admiral, Mar 15, 2005
    #4
    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. Christopher M. Lusardi
    Replies:
    1
    Views:
    4,080
  2. Martin Magnusson
    Replies:
    2
    Views:
    502
    John Harrison
    Oct 8, 2004
  3. Tor Erik Soenvisen
    Replies:
    14
    Views:
    556
    Tim Roberts
    Nov 23, 2006
  4. cuneyt
    Replies:
    2
    Views:
    264
    cuneyt
    Jul 15, 2005
  5. JustMe
    Replies:
    1
    Views:
    169
    Tassilo v. Parseval
    Aug 29, 2003
Loading...

Share This Page