Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 436
  • Last Modified:

lvalue c++ question

If a1, a2, and a3 are of a user defined type, T, and you overload the * operator for T, then it may be possible to write:

(a1*a2) = a3;

This looks non-sensical. Yet, it compiles and runs, but I think the statement behaves like a noop.

Could you show me from the C++ standard how this can be? I thought the LHS of assignment operator is an lvalue. But a1*a2 does not look like an lvalue to me. I don't see what possible usefulness the above statement could have.
0
phoffric
Asked:
phoffric
  • 4
  • 3
2 Solutions
 
jkrCommented:
>>But a1*a2 does not look like an lvalue to me.

If the overloaded operator yields a temporary instance of T that can be assigned a value, the expression would be valid yet...

>> I don't see what possible usefulness the above statement could have.

... not useful or even meaningful at all, as you wrote. Simply due to the temporary nature of the object.
0
 
phoffricAuthor Commented:
Thanks for your response.

From your remark, I got the impression that function returned values are temporary values which are also lvalues. Could you please show me in the C++ standard where this is and why it is necessary? Is it so that we can have statements like:   foo() = some-value; ?

There appears to be a difference between temporary values generated from User Defined Types and Built-In Types, as noted in this program:
template <class T>
class Lvalues {
public:
  Lvalues(T i) : val(i) {}
  T sumT(T arg) { (arg+val) = val; return (arg+val); }
  T val;
};

int main() {
  Lvalues<int> b1(4), b2(5), b3(6);
  Lvalues<string> a1("4"), a2("5"), a3("6");
  a1 = a2.sumT("12");  // OK, no compiler error
  cout << a1.val;
//  b1 = b2.sumT(12);
}

Open in new window

The line b1 = b2.sumT(12); has the compiler error: lvalue required as left operand of assignment. When commented out, the program gives the expected result: 125.

Why should it work for UDT, but not for Built-In-Type?
0
 
jkrCommented:
Well, I don't have the standard here ATM (holiday time ;o) - so I'll try to come up with what I still have at the top of my head. Basically, the compiler is not supposed to make any assumptions about UDTs, since they could indeed be or do anything (consider someone who wants to create some plausability check of some kind that way, and don't ask, I would not try it that way either for sheer readability reasons), but on the other hand has clear rules how to handle built-in types, with such a behaviour not being desired or even meaningful. But every operation that returns an object, that object is assumed to be assignable (which I assume to be "OO-101"), thus being fit for being taken as an L-value - and that's where the conundrum starts.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
phoffricAuthor Commented:
Ok, thanks for the comments. I will accept that you are representing the standard reasonably well. Thanks again, and Happy Holidays to you.
0
 
jkrCommented:
I hope I am (not 100% sure, but 90% should be OK - focusing on that very "temporary object"), but trying my best, and my hoilday wishs go back in your direction ;o)
0
 
jkrCommented:
Loking at that after a few days, let me make one correction:

I wrote

But every operation that returns an object, that object is assumed to be assignable (which I assume to be "OO-101"), thus being fit for being taken as an L-value - and that's where the conundrum starts.
and that should have been
every operation that returns a non-const object
- maybe 'constness' could be the key to that very conundrum here?
0
 
phoffricAuthor Commented:
>> every operation that returns a non-const object
Right, I figured that was what you meant. Thanks for clarifying.
Happy New Year!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now