Passing return value by reference

R

red floyd

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;
}
 
A

Andrey Tarasevich

red said:
...
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...
 
B

Bob Hairgrove

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.


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.
 
A

Andrey Tarasevich

Bob said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top