Is this legal?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Victor Bazarov

    #16
    Re: Is this legal?

    "Benoit Mathieu" <benoit.mathieu 2@NOSPAM.free.f r.invalid> wrote...[color=blue]
    > Victor Bazarov wrote:[color=green]
    > > So, binding a temporary _directly_ to a const reference is what causes
    > > the temporary to live as long as the reference[/color]
    >
    > I'll check this in my Stroustrup tomorrow...
    >[color=green]
    > > however, preserving
    > > that const reference by initialising a different const reference with
    > > it is NOT OK, since the lifetime of the first reference could be shorter
    > > than that of the different const reference. Did I understand the gist
    > > of your example correctly?[/color]
    >
    > I would formulate the rule like this:
    > ******
    > When you build a reference with a temporary object (like
    > Object & o = Object(...); )
    > the lifetime of Object is the same as the lifetime of the
    > reference o.[/color]

    This is incorrect. You cannot bind a terporary to a non-const reference.
    This is not allowed by the Standard and compliant compilers should complain.
    [color=blue]
    > *******
    > Claudio Jolowicz has another phrase to express this rule, if
    > you prefer... (18 Apr 2004 21:38:38 +0000 (UTC))[/color]

    It has nothing to do with what I prefer. It's mandated by the Standard.
    [color=blue]
    > [...][/color]


    Comment

    • Benoit Mathieu

      #17
      Re: Is this legal?

      >>Object & o = Object(...); )
      [color=blue]
      > This is incorrect. You cannot bind a terporary to a non-const reference.
      > This is not allowed by the Standard and compliant compilers should complain.[/color]

      Sorry, thanks for this rectification.. .

      Benoit

      Comment

      • Michael Sparks

        #18
        Re: Is this legal?

        "Claudio Jolowicz" <cj603@doc.ic.a c.uk> wrote in message
        news:Pine.LNX.4 .58.04041821570 40.14737@kiwi.d oc.ic.ac.uk...[color=blue][color=green]
        > >What I'm really interested in is *why* this is legal.[/color]
        >
        > There's nothing wrong about passing a temporary as a reference
        > parameter. Consider expression evaluation, where temporaries are passed
        > by reference all the time.
        >
        > How can the compiler guess that the programmer really wanted to bind
        > something else to the temporary using the parameter, and forgot it's the
        > parameter's lifetime that counts, and not the lifetime of that something
        > else?[/color]

        I suppose I was just disappointed that the language allows this to happen,
        due to its design.

        It's apparent that the intent with references was to relieve some of the
        responsibility the programmer has when dealing with pointers.
        E.g., you don't have to check for NULL, and (usually) you don't have to
        worry about them pointing to invalid places.

        The language makes it pretty difficult in general to make a bad reference,
        outside of writing *NULL.

        Perhaps it was unavoidable for compatibility reasons, but this seems to me
        like a blemish on an otherwise valuable language feature. In other words,
        it's another thing to add to the long list of "things that might bite you in
        the ass".

        Note to anyone in this group with obsessive/compulsive disorder, and/or any
        ego malfunctions: I'm not saying I could have done any better. :-)

        Mike


        Comment

        Working...