Why does this give compilation error?

Discussion in 'C++' started by doublemaster007@gmail.com, Jan 24, 2009.

  1. Guest

    string & fun()
    {
    return "Heello";
    }

    If string temp = "Hello" is ok..

    return "Heello"; should have created a temprory string object and that
    should have been returned. instead it gives compiler error.
     
    , Jan 24, 2009
    #1
    1. Advertising

  2. Kai-Uwe Bux Guest

    wrote:

    > string & fun()
    > {
    > return "Heello";
    > }
    >
    > If string temp = "Hello" is ok..


    You mean, if you do:

    string temp = "Heelo";
    return ( temp );

    ???

    > return "Heello"; should have created a temprory string object


    Right.

    > and that should have been returned.


    No. According to the signature, a _reference_ to that temporary should have
    been returned. And that is the problem: non-const references cannot be
    initialized from temporaries. See [8.5.3] for details.

    As for why the version with the temporary variable compiles: the variable is
    not a temporary, therefore you can initialize a reference from there.
    However, that string only lives to the end of fun(), and then the result is
    a dangling reference, the use of which will give you undefined behavior.

    I suggest, you ditch the "&" in the signature.

    > instead it gives compiler error.


    Right.


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Jan 24, 2009
    #2
    1. Advertising

  3. Guest

    On Jan 24, 9:29 am, Kai-Uwe Bux <> wrote:
    > wrote:
    > > string & fun()
    > > {
    > > return "Heello";
    > > }

    >
    > > If string temp = "Hello" is ok..

    >
    > You mean, if you do:
    >
    >   string temp = "Heelo";
    >   return ( temp );
    >
    > ???
    >
    > > return "Heello"; should have created a temprory string object

    >
    > Right.
    >
    > > and that should have been returned.

    >
    > No. According to the signature, a _reference_ to that temporary should have
    > been returned. And that is the problem: non-const references cannot be
    > initialized from temporaries. See [8.5.3] for details.
    >
    > As for why the version with the temporary variable compiles: the variable is
    > not a temporary, therefore you can initialize a reference from there.
    > However, that string only lives to the end of fun(), and then the result is
    > a dangling reference, the use of which will give you undefined behavior.
    >
    > I suggest, you ditch the "&" in the signature.
    >
    > > instead it gives compiler error.

    >
    > Right.
    >
    > Best
    >
    > Kai-Uwe Bux


    THank you sooo much.!

    I knew below is dangrous..but i was just wondring why the other code
    could not compile. I wasnt aware that refrences to temp object canot
    be created.
    > string temp = "Heelo";
    > return ( temp );
     
    , Jan 24, 2009
    #3
  4. Kai-Uwe Bux Guest

    wrote:

    > On Jan 24, 9:29 am, Kai-Uwe Bux <> wrote:
    >> wrote:
    >> > string & fun()
    >> > {
    >> > return "Heello";
    >> > }

    [snip]
    >> > return "Heello"; should have created a temprory string object

    >>
    >> Right.
    >>
    >> > and that should have been returned.

    >>
    >> No. According to the signature, a _reference_ to that temporary should
    >> have been returned. And that is the problem: non-const references cannot
    >> be initialized from temporaries. See [8.5.3] for details.

    [snip]
    > I knew below is dangrous..but i was just wondring why the other code
    > could not compile. I wasnt aware that refrences to temp object canot
    > be created.


    Well, that is a little too condensed. While temporaries cannot be used to
    initialize non-const references, such references to temporaries can be
    created under some circumstances. A member function could return that
    reference, and it is legal to call non-const member functions on
    temporaries. E.g.:

    std::vector<int>().operator=( some_int_vector );

    returns a non-const reference to a temporary vector<int> with specified
    value; and the temporary will live to the end of the expression.


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Jan 24, 2009
    #4
  5. Guest

    On Jan 24, 9:59 am, Kai-Uwe Bux <> wrote:
    > wrote:
    > > On Jan 24, 9:29 am, Kai-Uwe Bux <> wrote:
    > >> wrote:
    > >> > string & fun()
    > >> > {
    > >> > return "Heello";
    > >> > }

    > [snip]
    > >> > return "Heello"; should have created a temprory string object

    >
    > >> Right.

    >
    > >> > and that should have been returned.

    >
    > >> No. According to the signature, a _reference_ to that temporary should
    > >> have been returned. And that is the problem: non-const references cannot
    > >> be initialized from temporaries. See [8.5.3] for details.

    > [snip]
    > > I knew below is dangrous..but i was just wondring why the other code
    > > could not compile. I wasnt aware that refrences to temp object canot
    > > be created.

    >
    > Well, that is a little too condensed. While temporaries cannot be used to
    > initialize non-const references,


    Can we have refrence to temporaries if it is const?? i mean can const
    refrence be initialised by temporaroes?

    so would the below code work?? sorry i donot have a compiler to check
    this now..

    const string & fun()
    {
    return "Heello";
    }

    >such references to temporaries can be
    > created under some circumstances.

    Can you tell me some circmstatnces where we can have no-cost refrence
    to temporaries?

    >A member function could return that
    > reference, and it is legal to call non-const member functions on
    > temporaries. E.g.:


    Confused...! Cant we call const member function on the tempo's?? If it
    is const??
    >
    >   std::vector<int>().operator=( some_int_vector );
    >
    > returns a non-const reference to a temporary vector<int> with specified
    > value; and the temporary will live to the end of the expression.


    As far as i know..tempo's will live till the outer scope ends..[not
    till the end of expression] am i worng??
    >
    > Best
    >
    > Kai-Uwe Bux- Hide quoted text -
    >
    > - Show quoted text -



    Is there any way i can understand C++ specifications? Every time i
    read..i find it diff to understand..so i read more books on C++ and
    comes back..still the result is same..Are there any links, refereces
    or documents which helps to parse specification easily?? I really
    wanna understand that..pls help me..
     
    , Jan 25, 2009
    #5
  6. * :
    > On Jan 24, 9:59 am, Kai-Uwe Bux <> wrote:
    >> wrote:
    >>> On Jan 24, 9:29 am, Kai-Uwe Bux <> wrote:
    >>>> wrote:
    >>>>> string & fun()
    >>>>> {
    >>>>> return "Heello";
    >>>>> }

    >> [snip]
    >>>>> return "Heello"; should have created a temprory string object
    >>>> Right.
    >>>>> and that should have been returned.
    >>>> No. According to the signature, a _reference_ to that temporary should
    >>>> have been returned. And that is the problem: non-const references cannot
    >>>> be initialized from temporaries. See [8.5.3] for details.

    >> [snip]
    >>> I knew below is dangrous..but i was just wondring why the other code
    >>> could not compile. I wasnt aware that refrences to temp object canot
    >>> be created.

    >> Well, that is a little too condensed. While temporaries cannot be used to
    >> initialize non-const references,

    >
    > Can we have refrence to temporaries if it is const?? i mean can const
    > refrence be initialised by temporaroes?


    Yes.


    > so would the below code work?? sorry i donot have a compiler to check
    > this now..
    >
    > const string & fun()
    > {
    > return "Heello";
    > }


    Nope, it's UB.

    The current rules are simple in terms of what has to happen at the machine code
    level -- no "magic" introduced by the compiler.

    The above (which your compiler has to accept, but may warn about) means that a
    temporary string instance is created in the function call's stack frame, a
    reference to that instance is copied as result, and the instance is destroyed as
    part of the function return. Thus, the reference is at this point invalid. About
    the only thing it can be used for is to obtain an address in the stack.


    >> such references to temporaries can be
    >> created under some circumstances.

    > Can you tell me some circmstatnces where we can have no-cost refrence
    > to temporaries?


    Not sure what circumstances Kai-Uwe was thinking of, but it seems reasonable
    that the example he then immediately presented, and which you're quoting below,
    may have something to do with it, don't you agree?


    >> A member function could return that
    >> reference, and it is legal to call non-const member functions on
    >> temporaries. E.g.:

    >
    > Confused...! Cant we call const member function on the tempo's?? If it
    > is const??


    The basic rule is that is that a reference to non-const can't be bound to a
    value (like, "int& r = 42"), because that reference could then be used to modify
    the value.


    >> std::vector<int>().operator=( some_int_vector );
    >>
    >> returns a non-const reference to a temporary vector<int> with specified
    >> value; and the temporary will live to the end of the expression.

    >
    > As far as i know..tempo's will live till the outer scope ends..[not
    > till the end of expression] am i worng??'


    Temporaries introduced in an expression are guaranteed destroyed at the end of
    the full-expression (with two exceptions: used to initialize a declared object,
    and used to initialize a reference).


    > Is there any way i can understand C++ specifications? Every time i
    > read..i find it diff to understand..so i read more books on C++ and
    > comes back..still the result is same..Are there any links, refereces
    > or documents which helps to parse specification easily?? I really
    > wanna understand that..pls help me..


    There may be FAQ on this, and perhaps some web-sites dedicated to the issue.


    Cheers, & hth.,

    - Alf
     
    Alf P. Steinbach, Jan 25, 2009
    #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. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,994
    Smokey Grindel
    Dec 2, 2006
  2. grocery_stocker
    Replies:
    10
    Views:
    631
    Keith Thompson
    May 25, 2005
  3. Paul Melis

    Why oh why does this NOT give a compile error?

    Paul Melis, Nov 5, 2007, in forum: C Programming
    Replies:
    13
    Views:
    521
    cr88192
    Nov 7, 2007
  4. Replies:
    5
    Views:
    105
  5. FAQ server
    Replies:
    24
    Views:
    313
    Randy Webb
    Sep 20, 2006
Loading...

Share This Page