Re: Passing return value by reference

From: Andrey Tarasevich (andreytarasevich_at_hotmail.com)
Date: 06/14/04


Date: Mon, 14 Jun 2004 11:02:48 -0700

Bob Hairgrove wrote:
> On Fri, 11 Jun 2004 11:24:13 -0700, Andrey Tarasevich
> <andreytarasevich@hotmail.com> wrote:
>
>>red floyd wrote:
>>> ...
>>> Am I allowed to pass the anonymous temporary returned by f() to a
>>> function requiring a non-const reference?
>>
>>No.
>>
>>> I suspect G++ is correct, and VC is (again) braindead.
>>
>>VC supports this as an extension. The only reason it compiles on VC is
>>that you have this extension enabled. Disable it and VC will also issue
>>an error message.
>>
>>> class A
>>> {
>>> public:
>>> A() { }
>>> A(const A&) { }
>>> ~A() { }
>>> };
>>>
>>> A& operator<<(A& a, const char* p)
>>> {
>>> return a;
>>> }
>>>
>>> A f()
>>> {
>>> return A();
>>> }
>>>
>>> int main()
>>> {
>>> f() << "Hello";
>>> return 0;
>>> }
>>
>>You can make this compile by implementing 'operator<<' as a member
>>function of class 'A'. There's certain asymmetry in C++ behavior when it
>>comes to things like this...
>
> Doesn't the problem actually stem from the temporary A which is
> created from the temporary returned by f() in order to bind to the
> non-const reference argument declared by operator<<()?

Yes. That's the immediate reason for the error message, as was already
stated above. However, one way to work around the problem is, once
again, to implement 'operator<<' as a member function of class 'A'.

  class A {
  public:
    ...
    A& operator<<(const char* p) { return *this; }
  };

  A f() {
    return A();
  }

  int main() {
    f() << "Hello"; // OK, no error
    return 0;
  }

C++ permits non-constant member function calls on temporary objects. I
can't say whether this is a viable solution in OP's case because all I
have is the above piece of abstract code.

-- 
Best regards,
Andrey Tarasevich


Relevant Pages

  • Re: How to place focus back after error message window closed?
    ... Well, the reason the code doesn't put the focus back where you want it is that, well, the ... I've even used a tooltip, ... The use of string variables suggests you are using UpdateData to store the values. ... If one of them is empty and user click OK or press Enter, an error message ...
    (microsoft.public.vc.mfc)
  • Re: Saving Documents in Word 2002 using Vista
    ... Depending on the version of Word you are using the error message is similar to what you provided. ... Microsoft Office MVP ... "ozimac" wrote in message ... but now for some reason it won't do it. ...
    (microsoft.public.word.docmanagement)
  • Re: Loading single word to a xmm register
    ... And you know the reason why?... ... As long as i have written an Assembler that compiles ... optimize the Routine at the Code Level intensively, ...
    (alt.lang.asm)
  • Was there a negative real zero possibility in early Fortran?
    ... 99 (WRITE ERROR MESSAGE) ... I already have found severe errors; one example is this original code ... I know this version was written for an IBM 360 in Fortran H (Fortran ... Is there any reason why the programmer is testing in some cases, ...
    (comp.lang.fortran)
  • Re: New, "improved" Ancestry marriage search not working properly
    ... Spouse" facility is unable to cope and returns a "Your search ... ... returned no matches" error message. ... the reason it won't find the match is that it appears to ...
    (soc.genealogy.britain)