Solved

lvalue c++ question

Posted on 2012-12-25
7
411 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction This article discusses the Chain of Responsibility pattern, explaining What it is;Why it is; andHow it is At the end of this article, I hope you will be able to describe the use and benefits of Chain of Responsibility.  Backgrou…
Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now