By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,138 Members | 1,233 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,138 IT Pros & Developers. It's quick & easy.

Passing return value by reference

P: n/a
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;
}
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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

Jul 22 '05 #2

P: n/a
On Fri, 11 Jun 2004 11:24:13 -0700, Andrey Tarasevich
<an**************@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<<()?

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
No**********@Home.com
Jul 22 '05 #3

P: n/a
Bob Hairgrove wrote:
On Fri, 11 Jun 2004 11:24:13 -0700, Andrey Tarasevich
<an**************@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

Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.