Solved

lvalue c++ question

Posted on 2012-12-25
7
416 Views
Last Modified: 2012-12-27
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
Comment
Question by:phoffric
  • 4
  • 3
7 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 250 total points
ID: 38720280
>>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
 
LVL 32

Author Comment

by:phoffric
ID: 38720426
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
 
LVL 86

Assisted Solution

by:jkr
jkr earned 250 total points
ID: 38720481
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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 32

Author Closing Comment

by:phoffric
ID: 38720507
Ok, thanks for the comments. I will accept that you are representing the standard reasonably well. Thanks again, and Happy Holidays to you.
0
 
LVL 86

Expert Comment

by:jkr
ID: 38720541
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
 
LVL 86

Expert Comment

by:jkr
ID: 38725919
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
 
LVL 32

Author Comment

by:phoffric
ID: 38726061
>> 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction A frequently used term in Object-Oriented design is "SOLID" which is a mnemonic acronym that covers five principles of OO design.  These principles do not stand alone; there is interplay among them.  And they are not laws, merely princ…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

837 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question