Explanation for rvalue/lvalue

C

Christian Meier

Hi NG

I'm using "lint" and got a message which I do not understand.
When I "lint" the following code I get the message "Initializing a non-const
reference C &' with a non-lvalue".

class C {
public:
static C GetTemporaryInstance() { return C(); }
C& operator<<(int) { return *this; }
private:
C() {}
};

int main()
{
C::GetTemporaryInstance() << 5;
}


1. Can you explain me the lint message? Where am I initializing a C
reference?
2. Is this code legal c++ or is it not allowed to call the operator<< of a
temporary object like the return value of C::GetTemporaryInstance()?

Thanks for your answers and regards,
Chris
 
V

Victor Bazarov

Christian said:
I'm using "lint" and got a message which I do not understand.
When I "lint" the following code I get the message "Initializing a
non-const reference C &' with a non-lvalue".

class C {
public:
static C GetTemporaryInstance() { return C(); }
C& operator<<(int) { return *this; }
private:
C() {}
};

int main()
{
C::GetTemporaryInstance() << 5;
}


1. Can you explain me the lint message? Where am I initializing a C
reference?

You're returning a reference to non-const from operator<<.
2. Is this code legal c++ or is it not allowed to call the operator<<
of a temporary object like the return value of
C::GetTemporaryInstance()?

I am uncertain whether the code is fully legal; a non-const reference
is bound to the temporary object which is not allowed. But this is
a common idiom... Begs the question about the validity of the idiom,
doesn't it?

V
 
A

Alf P. Steinbach

* Victor Bazarov:
You're returning a reference to non-const from operator<<.

That seems to be what lint triggers on.

However, the initializer expression there /is/ an lvalue.

So it must be that lint takes into account the usage in main.

I am uncertain whether the code is fully legal; a non-const reference
is bound to the temporary object which is not allowed. But this is
a common idiom... Begs the question about the validity of the idiom,
doesn't it?

I can't see anything invalid.


Cheers,

- Alf
 
A

Andrey Tarasevich

Christian said:
I'm using "lint" and got a message which I do not understand.
When I "lint" the following code I get the message "Initializing a non-const
reference C &' with a non-lvalue".

class C {
public:
static C GetTemporaryInstance() { return C(); }
C& operator<<(int) { return *this; }
private:
C() {}
};

int main()
{
C::GetTemporaryInstance() << 5;
}


1. Can you explain me the lint message? Where am I initializing a C
reference?

You are initializing a reference when you return it from 'C::eek:perator<<'.

Note though that formally speaking there's not a single spot in your
code where a reference would be initialized with a non-lvalue. The
result of '*this' is an lvalue. The result of built-in unary '*' always is.

Lint complains in this case probably because it performed a deeper
context-dependent analysis of your code and discovered that when
'C::eek:perator <<' is called from 'C::GetTemporaryInstance() << 5'
context, 'this' pointer actually points to a temporary object. Which
means that in the end the returned reference gets bound to a temporary
object. Trying to store the returned reference and use it later would
result in disaster. This is probably what it wants to warn you about.

But in your code the reference is not stored, meaning that the code is
perfectly safe. Which is why it looks strange to me that having such a
context-dependent analysis capability lint failed to see that there's no
problem here.
2. Is this code legal c++ or is it not allowed to call the operator<< of a
temporary object like the return value of C::GetTemporaryInstance()?

This is perfectly legal.
 
W

witmer

Begs the question about the validity of the idiom,
doesn't it?

It does no such thing. To quote my favorite coffee cup, begging the
question is:

A logical fallacy in which an argument is assumed to be true without
any evidence other than the argument itself. It does not mean "to
raise the question."
 
V

Victor Bazarov

witmer said:
It does no such thing. To quote my favorite coffee cup, begging the
question is:

A logical fallacy in which an argument is assumed to be true without
any evidence other than the argument itself. It does not mean "to
raise the question."

Not sure what you're picking on. Please elaborate. If you have
another coffee mug to quote, feel free.

V
 
C

Christian Meier

Thanks for your helpful answers, Victor, Alf and Andrey.

[...]
static C GetTemporaryInstance() { return C(); }
C& operator<<(int) { return *this; }
[...]
You're returning a reference to non-const from operator<<.

That seems to be what lint triggers on.

However, the initializer expression there /is/ an lvalue.

I think we all agree that "C::GetTemporaryInstance()" is a rvalue and
"*this" is normally an lvalue.
But in this context "*this" is the same object as the return value of
"C::GetTemporaryInstance()".
Does the context not count for deciding whether an expression is an lvalue
or an rvalue?
I can't see anything invalid.

I looked up the lint manual for an explanation of this error message. The
text relies on the assumption that the initializer is an rvalue:
"Initializing a non-const reference 'Symbol' with a non-lvalue -- A
reference is normally initialized with an lvalue. If you attempt to
initialize a reference with a non-lvalue, a temporary is created to serve as
a surrogate lvalue. However, modifications made to the temporary will be
lost. This was legal at one time and is now illegal. [...]"

I think I will suppress the warning.

Best regards,
Chris
 
V

Victor Bazarov

Erik said:

So, it's difference between prescriptive and descriptive linguistics,
right? WP says that it's incorrect, yet the use is "increasing". So
now 'witmer's coffee mug is making attempt to demonstrate that my use
of "begs the question" is improper, right. Quite possible, English
is not my native langauge. I wish 'witmer' luck in catching more of
my incorrect usages in the future!

V
 
E

Erik Wikström

So, it's difference between prescriptive and descriptive linguistics,
right? WP says that it's incorrect, yet the use is "increasing". So
now 'witmer's coffee mug is making attempt to demonstrate that my use
of "begs the question" is improper, right. Quite possible, English
is not my native langauge. I wish 'witmer' luck in catching more of
my incorrect usages in the future!

:)

Personally I would say that there is a difference between "begs the
question" and "begging the question", but then English is not my native
language either.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top