473,382 Members | 1,387 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Passing return value by reference

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
3 1668
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
25
by: Victor Bazarov | last post by:
In the project I'm maintaining I've seen two distinct techniques used for returning an object from a function. One is AType function(AType const& arg) { AType retval(arg); // or default...
13
by: Abe Frohnman | last post by:
Hello all, I'm passing a reference to a class into the constructor of a form, like so: public MyForm(int count, ref Area myArea) {...} How can I use myArea outside the constructor? Should I...
7
by: Ken Allen | last post by:
I have a .net client/server application using remoting, and I cannot get the custom exception class to pass from the server to the client. The custom exception is derived from ApplicationException...
9
by: Just Me | last post by:
PARAFORMAT2 is a structure that SendMessage will return stuff in. Is the "ref" correct or since only a pointer is being passed should it be by value? Suppose I was passing data rather then...
6
by: ged | last post by:
Hi, i am a oo (c#) programmer, and have not used javascript for a while and i cant work out how javascript manages its references. Object References work for simple stuff, but once i have an...
12
by: Andrew Bullock | last post by:
Hi, I have two classes, A and B, B takes an A as an argument in its constructor: A a1 = new A(); B b = new B(a1);
12
by: dave_dp | last post by:
Hi, I have just started learning C++ language.. I've read much even tried to understand the way standard says but still can't get the grasp of that concept. When parameters are passed/returned...
8
by: S. | last post by:
Hi all, Can someone please help me with this? I have the following struct: typedef struct { char *name; int age; } Student;
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.