473,508 Members | 4,779 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Return by value -- primitive type vs class type

Please consider the following...

<CODE>

#include <string>
using namespace std;

typedef int PrimitiveType;

typedef string ClassType;

PrimitiveType ReturnPrimitiveType()
{
PrimitiveType result;
return result;
}

ClassType ReturnClassType()
{
ClassType result;
return result;
}

int main()
{
PrimitiveType pt;
ClassType ct;

ReturnPrimitiveType() = pt; //ERROR!

ReturnClassType() = ct; //OKAY!

return 0;
}

</CODE>

The first function call in main() generates the error "left operand must be
l-value."

The second function call in main() does not.

Both functions return their results by value. The only difference between the
functions is that the first returns a primitive type and the second returns a
class type.

Both are obviously temporary objects, but my compiler seems to consider the
temporary of class type to be an l-value.

The primitive return type seems implicitly const whereas with the class type
return type I need to explicitly specify const.

Is that the correct behavior?

Why allow modification of the class type temporary but disallow it for the
primitive temporary?

It is not important to some specific problem I have, I just like knowing why
languages work the way they do.

Regards,
Brian

Jul 22 '05 #1
8 3063
On 13 May 2004 19:33:37 GMT, da*********@aol.com (DaKoadMunky) wrote:
Please consider the following...

<CODE>

#include <string>
using namespace std;

typedef int PrimitiveType;

typedef string ClassType;

PrimitiveType ReturnPrimitiveType()
{
PrimitiveType result;
return result;
}

ClassType ReturnClassType()
{
ClassType result;
return result;
}

int main()
{
PrimitiveType pt;
ClassType ct;

ReturnPrimitiveType() = pt; //ERROR!

ReturnClassType() = ct; //OKAY!

return 0;
}

</CODE>

The first function call in main() generates the error "left operand must be
l-value."

The second function call in main() does not.

Both functions return their results by value. The only difference between the
functions is that the first returns a primitive type and the second returns a
class type.

Both are obviously temporary objects, but my compiler seems to consider the
temporary of class type to be an l-value.
Not an lvalue - something else. Not sure what the term is, but here's what
the Standard says (3.10/2):

"An lvalue refers to an object or function. Some rvalue
expressions--those of class or cv-qualified class type--also refer to
objects. [47]

and footnote 47 says:

"Expressions such as invocations of constructors and of functions that
return a class type refer to objects, and the implementation can invoke
a member function upon such objects, but the expressions are not
lvalues."

The primitive return type seems implicitly const whereas with the class type
return type I need to explicitly specify const.

Is that the correct behavior?
Evidently so.

Why allow modification of the class type temporary but disallow it for the
primitive temporary?
Well, it would after all convenient to be able to do stuff like:

getApp().run();

(although getApp()->run() might be more intuitive).

Or how about:

cout << cstr_to_string_with_munging(someCstr).insert(0, '*') << endl;

or even (according to footnote 47):

cout << string("foobar").insert(0, '*');

Since unnamed object return values have a location associated with them,
and that location may be used as a "this" pointer to member function calls,
why not allow it? OTOH, unnamed primitive return values don't have
addresses, and thus it wouldn't make sense to allow operations that modify
such objects.

It is not important to some specific problem I have, I just like knowing why
languages work the way they do.
So do I ;-)
-leor

Regards,
Brian


--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #2
>Since unnamed object return values have a location associated with them,
and that location may be used as a "this" pointer to member function calls,
why not allow it?
I thought maybe by default that *this for an unnamed object would be const,
allowing reading of the object but not writing it. This seems more like the
"do as the ints" do approach I have heard advocated at times.
OTOH, unnamed primitive return values don't have addresses


Sounds like my parents. When I ask to come visit they claim they don't have an
address.

Seriously though, unless the return value is a value that can be fixed at
compile time the return value has to have a location that can be written to!
On my implementation it seems to use registers, but that is a location
nonetheless.

Thanks for your time.

Jul 22 '05 #3

"DaKoadMunky" <da*********@aol.com> wrote in message
news:20***************************@mb-m16.aol.com...
Since unnamed object return values have a location associated with them,
and that location may be used as a "this" pointer to member function calls,why not allow it?
I thought maybe by default that *this for an unnamed object would be

const, allowing reading of the object but not writing it. This seems more like the "do as the ints" do approach I have heard advocated at times.
What's constant is the return value, which for an object is really a
reference to data stored elsewhere. So, although the address of that
unnamed object cannot be changed, any of the members of the object it refers
to *can* be changed, in this case via the assignment operator (a function
call). On the other hand, an assignment to an integer (or other primitive
type) return value actually attempts to change the return value itself,
which is not allowed.

Seriously though, unless the return value is a value that can be fixed at
compile time the return value has to have a location that can be written to! On my implementation it seems to use registers, but that is a location
nonetheless.


It's a location, but it's not writeable in this sense. For an object, it
refers to data stored elsewhere. For a primitive type, it *is* the data.
Thus the difference in allowable behavior.

-Howard

Jul 22 '05 #4
On Fri, 14 May 2004 14:47:01 GMT, "Howard" <al*****@hotmail.com> wrote:

"DaKoadMunky" <da*********@aol.com> wrote in message
news:20***************************@mb-m16.aol.com...
>Since unnamed object return values have a location associated with them,
>and that location may be used as a "this" pointer to member functioncalls, >why not allow it?


I thought maybe by default that *this for an unnamed object would be

const,
allowing reading of the object but not writing it. This seems more like

the
"do as the ints" do approach I have heard advocated at times.


What's constant is the return value, which for an object is really a
reference to data stored elsewhere.


Are you talking about how you think the compiler is (internally)
implementing return-by-value semantics for objects, or are you referring to
the case where the return type is actually declared as a reference? If the
latter, OK; if the former, however, I say "not necessarily" (and for all I
know, that may even be the case for the latter). Consider this program:

//
// smallobj.cpp:
//

#include <iostream>
using namespace std;

class small {
public:
small(int n) : value(n) {}
int getValue() const { return value; }
int operator++() { return ++value; }
friend ostream &operator<<(ostream &os, const small &s)
{
return os << s.value;
}

private:
int value;
};

small makeSmall(int n)
{
return small(n);
}

int main()
{
cout << ++makeSmall(10) << endl;
return 0;
}
Output: 11

Are there actually any guarantees that the "value" coming back from
makeSmall() isn't in a register? I don't think so. If that's how the code
generated lays it out, so much the better.
-leor

--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #5

"Leor Zolman" <le**@bdsoft.com> wrote in message
news:18********************************@4ax.com...
On Fri, 14 May 2004 14:47:01 GMT, "Howard" <al*****@hotmail.com> wrote:

"DaKoadMunky" <da*********@aol.com> wrote in message
news:20***************************@mb-m16.aol.com...
>Since unnamed object return values have a location associated with them, >and that location may be used as a "this" pointer to member functioncalls,
>why not allow it?

I thought maybe by default that *this for an unnamed object would be

const,
allowing reading of the object but not writing it. This seems more
likethe
"do as the ints" do approach I have heard advocated at times.


What's constant is the return value, which for an object is really a
reference to data stored elsewhere.


Are you talking about how you think the compiler is (internally)
implementing return-by-value semantics for objects, or are you referring

to the case where the return type is actually declared as a reference? If the
latter, OK; if the former, however, I say "not necessarily" (and for all I
know, that may even be the case for the latter). Consider this program:

I'm not really describing either a reference (or a const) as such, just
trying to point out that when an object is returned, regardless of how it is
actually implememented (e.g., in a register), you can't *change* that value,
any more than you can change it if it's a primitive type. But you *can*
access its members. What I was trying to say, I guess, was that the object
is not actually sitting in that register or unnamed location, because it is
(at least potentially) too big. Rather, the compiler places an address
there (wherever *there* is), and that address points to the actual data.
This is true of return semantics as well as parameters passed by value: when
passing an object by value, the address of the object is actually passed,
not the contents of the object. That fact is what enables you to make use
of the address to call functions or access members of the object.

-Howard


Jul 22 '05 #6
On Fri, 14 May 2004 17:58:30 GMT, "Howard" <al*****@hotmail.com> wrote:

"Leor Zolman" <le**@bdsoft.com> wrote in message
news:18********************************@4ax.com.. .
Are you talking about how you think the compiler is (internally)
implementing return-by-value semantics for objects, or are you referring

to
the case where the return type is actually declared as a reference? If the
latter, OK; if the former, however, I say "not necessarily" (and for all I
know, that may even be the case for the latter). Consider this program:


I'm not really describing either a reference (or a const) as such, just
trying to point out that when an object is returned, regardless of how it is
actually implememented (e.g., in a register), you can't *change* that value,
any more than you can change it if it's a primitive type.


Did you take a look at that program I posted? What is the line:

cout << ++makeSmall(10) << endl;

doing if not "changing the value" of the object returned by makeSmall() ?

You wouldn't have been able to do that if the return value was a primitive
type, but you /can/ do it with a user-defined type.
But you *can*
access its members. What I was trying to say, I guess, was that the object
is not actually sitting in that register or unnamed location, because it is
(at least potentially) too big. Rather, the compiler places an address
there (wherever *there* is), and that address points to the actual data.
This is true of return semantics as well as parameters passed by value: when
passing an object by value, the address of the object is actually passed,
not the contents of the object. That fact is what enables you to make use
of the address to call functions or access members of the object.
And what I was trying to say is you can't say any of that.
-leor

-Howard



--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #7

"Leor Zolman" <le**@bdsoft.com> wrote in message
news:d9********************************@4ax.com...
On Fri, 14 May 2004 17:58:30 GMT, "Howard" <al*****@hotmail.com> wrote:

"Leor Zolman" <le**@bdsoft.com> wrote in message
news:18********************************@4ax.com.. .
Are you talking about how you think the compiler is (internally)
implementing return-by-value semantics for objects, or are you referring
to
the case where the return type is actually declared as a reference? If
the latter, OK; if the former, however, I say "not necessarily" (and for all I know, that may even be the case for the latter). Consider this program:


I'm not really describing either a reference (or a const) as such, just
trying to point out that when an object is returned, regardless of how it

isactually implememented (e.g., in a register), you can't *change* that value,any more than you can change it if it's a primitive type.


Did you take a look at that program I posted? What is the line:

cout << ++makeSmall(10) << endl;

doing if not "changing the value" of the object returned by makeSmall() ?


That statement is calling the object's operator ++(), which is simply a
function call. The value that gets changed its member variable "value", not
the return value of makeSmall itself. And calling a function on that object
is perfectly legal, whether or not that function changes any of the object's
member values.

The reason I used the term "reference" earlier is that it is the same as if
you had declared and initialized a reference variable. You can't tell that
variable that it now refers to something else (thus changing the variable),
but you *can* access the object's members freely.

As far as I know, there is not even any way (semantically) to change the
value that's returned from a function when that value is an object (even if
the compiler would allow it), because all operations on that object are
implemented as operators, which are simply function calls.

You wouldn't have been able to do that if the return value was a primitive
type, but you /can/ do it with a user-defined type.
But you *can*
access its members. What I was trying to say, I guess, was that the objectis not actually sitting in that register or unnamed location, because it is(at least potentially) too big. Rather, the compiler places an address
there (wherever *there* is), and that address points to the actual data.
This is true of return semantics as well as parameters passed by value: whenpassing an object by value, the address of the object is actually passed,
not the contents of the object. That fact is what enables you to make useof the address to call functions or access members of the object.


And what I was trying to say is you can't say any of that.


? And I thought I was in agreement with you in this thread. Are you now
disputing that fact that you can access members and functions of a returned
object but cannot modify primitives returned by value? Or are you simply
saying that the way I've described it *may* be how it is implemented, but
not how it is *required* to be implemented?

-Howard
Jul 22 '05 #8
On Fri, 14 May 2004 18:31:09 GMT, "Howard" <al*****@hotmail.com> wrote:

"Leor Zolman" <le**@bdsoft.com> wrote in message
news:d9********************************@4ax.com.. .
Did you take a look at that program I posted? What is the line:

cout << ++makeSmall(10) << endl;

doing if not "changing the value" of the object returned by makeSmall() ?
That statement is calling the object's operator ++(), which is simply a
function call.


Yes, it technically invokes an operator function, but the semantics still
reflect a direct modification of an rvalue that's being returned from the
function.
The value that gets changed its member variable "value", not
the return value of makeSmall itself. And calling a function on that object
is perfectly legal, whether or not that function changes any of the object's
member values.

The reason I used the term "reference" earlier is that it is the same as if
you had declared and initialized a reference variable. You can't tell that
variable that it now refers to something else (thus changing the variable),
but you *can* access the object's members freely.
Right; and I was focusing on the scenario when it isn't any kind of a
reference, but actually just a temporary in wherever place the compiler
sees fit to put it for the best performance. In a register, for example.
Applying ++ to it, even though it is going "through" a member function
(semantically at least, but inlined) would actually be a direct
modification to the object, compiling into code that just increments the
value in that alleged register.

As far as I know, there is not even any way (semantically) to change the
value that's returned from a function when that value is an object (even if
the compiler would allow it), because all operations on that object are
implemented as operators, which are simply function calls.
I don't know; I read that passage I quoted from the Standard at the start
of this thread one way, now I detect some wiggle room in the wording (so
what else is new?) but I have a hard time getting the image of that object
being changed "directly" by the ++ operator out of my head...

You wouldn't have been able to do that if the return value was a primitive
type, but you /can/ do it with a user-defined type.
> But you *can*
>access its members. What I was trying to say, I guess, was that theobject >is not actually sitting in that register or unnamed location, because itis >(at least potentially) too big. Rather, the compiler places an address
>there (wherever *there* is), and that address points to the actual data.
>This is true of return semantics as well as parameters passed by value:when >passing an object by value, the address of the object is actually passed,
>not the contents of the object. That fact is what enables you to makeuse >of the address to call functions or access members of the object.
And what I was trying to say is you can't say any of that.


? And I thought I was in agreement with you in this thread. Are you now
disputing that fact that you can access members and functions of a returned
object but cannot modify primitives returned by value?


No, I never meant to give the impression I disputed those things. I just
have trouble making a distinction between invoking a mutating member
function vs. "modifying the object". It seems the two go hand-in-hand; how
can you successfully invoke a mutating function, yet not "modify the
object", or, in your words, " change the value that's returned from a
function when that value is an object" ?
Or are you simply
saying that the way I've described it *may* be how it is implemented, but
not how it is *required* to be implemented?
That I /was/ definitely saying. I got the feeling your argument was based
on the actual bits of the return value being a pointer/ref of some kind,
and I was trying to show that this need not be the scenario; in the
scenario I'm thinking about, the actual object is returned "directly" and
operated upon "directly". I just don't see how the C++ language mechanism
of "applying a member function" (such as an inlined operator++()) somehow
removes the "directness" of the operation such that you can no longer
correctly say you're modifying the object when you apply such a function.

I must admit I'm speaking from the gut here, and am not familiar enough
with the Standardese that defines these mechanisms formally. In some of the
past cases where I've gotten involved in conceptual discussions like this,
I eventually learned the vocabulary that proved me wrong, or at least
proved I'd been using the wrong words. I wouldn't be the least bit
surprised if the same thing ends up happening here...
-leor

-Howard


--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2716
by: case2005 | last post by:
Can anyone help with the following, I don't know if it's possible, but I'm certain there must be a standard way of dealing with this. I have the following: template<typename FooBar, typename...
16
25390
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have...
1
4120
by: dpakpaul | last post by:
Hi, I have an XSD schema where I have attributes that are declared to contain non string values such as integers etc. Take for example, this declaration - <xs:attribute name="IsThisTrue"...
3
1470
by: Herve Bocuse | last post by:
Hi, I have a question dealing to value and reference types, boxing and unboxing. I'm coming from the C++ world so I don't see how to do the following feature: I have several primitive types...
5
2077
by: Zach | last post by:
When it is being said that, "value types are created on the stack or inline as part of an object". If a value type is created in an object, and that object is being called, the value type in that...
1
2840
by: louis_la_brocante | last post by:
Dear all, I am having trouble generating a client proxy for a webservice whose methods return a "complex" type. The type is complex in that it is a class whose members are a mix of primitive...
5
8064
by: howa | last post by:
Hi, Consider a simple example, e.g. var a = { 'a': 'b', 'c': 'd' }
14
1746
by: KK | last post by:
Dear All I have a small problem with using as operator on value type array. Here is an example what I am trying to do. using System; using System.Collections.Generic; using System.Text;
4
7128
by: Jonathan | last post by:
I have a SQL stored procedure for adding a new record in a transactions table. It also has two return values: CounterID and IDKey. I want to create a webservice that accepts the 10 input...
0
7228
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7128
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7332
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7393
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7502
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5057
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3191
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
769
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
426
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.