Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Passing return value by reference

Reply
Thread Tools

Passing return value by reference

 
 
red floyd
Guest
Posts: n/a
 
      06-11-2004
Once again, I'm fighting a port from the (allegedly standard compliant)
VC7.1 to G++.

VC compiles this, G++ doesn't.

Am I allowed to pass the anonymous temporary returned by f() to a
function requiring a non-const reference? I suspect G++ is correct, and
VC is (again) braindead.


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;
}
 
Reply With Quote
 
 
 
 
Andrey Tarasevich
Guest
Posts: n/a
 
      06-11-2004
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...

--
Best regards,
Andrey Tarasevich

 
Reply With Quote
 
 
 
 
Bob Hairgrove
Guest
Posts: n/a
 
      06-12-2004
On Fri, 11 Jun 2004 11:24:13 -0700, Andrey Tarasevich
<(E-Mail Removed)> 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<<()?

The standard says that object return values are always rvalues.

If we rewrite f() thus:

A& f()
{
static A a;
return a;
}

then it should compile.

Whether or not this is a good design is yet another issue.


--
Bob Hairgrove
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      06-14-2004
Bob Hairgrove wrote:
> On Fri, 11 Jun 2004 11:24:13 -0700, Andrey Tarasevich
> <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
return type vs passing a reference Carl Forsman C++ 13 11-17-2008 10:10 AM
What to return -- object, reference or const reference Arv C++ 15 03-07-2008 09:15 PM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM
Passing return as reference to another function Ryan Mitchley C++ 2 10-16-2003 03:37 PM
Passing the value by reference is same as pointer by reference sam pal C++ 3 07-16-2003 09:14 PM



Advertisments