473,513 Members | 2,339 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A non-const reference may only be bound to an lvalue?

Hello everyone,
I am debugging MSDN code from,

http://msdn2.microsoft.com/en-us/lib...ah(VS.80).aspx

here is my output,

1>main.cpp
1>d:\visual studio 2008\projects\test_c4350\test_c4350\main.cpp(21) :
warning C4350: behavior change: 'B::B(A)' called instead of 'B::B(B
&)'
1 d:\visual studio 2008\projects
\test_c4350\test_c4350\main.cpp(13) : see declaration of 'B::B'
1 d:\visual studio 2008\projects
\test_c4350\test_c4350\main.cpp(9) : see declaration of 'B::B'
1 A non-const reference may only be bound to an lvalue

I do not quite understand why there is a warning A non-const reference
may only be bound to an lvalue and 'B::B(A)' called instead of 'B::B(B
&)'? Could anyone give some descriptions please?
thanks in advance,
George
Dec 14 '07 #1
3 4089
George2 <ge*************@yahoo.comwrote in comp.lang.c++:
I do not quite understand why there is a warning A non-const reference
may only be bound to an lvalue?

A const reference can be bound to:
R-value
L-value

A non-const reference can be bound to:
L-value

This means that you can do this:

int const &x = 5;

But you _can't_ do this:

int &x = 5;

, thus preventing you from trying to modify a literal, or any kind of R-
value for that matter.

I realise you can have a non-const R-value, (such as if a function call
were to return a non-const class object), but I don't think it it'd be a
good idea to have non-const references to R-values floating around,
especially if these non-const references didn't "extend the lifetime" of
the R-value (which is likely to be a temporary a lot of the time).

The way I like to understand binding a const reference to an R-value is
that the following two are identical:

ClassType const &x = FunctionReturnsObject();

and:

ClassType const ret = FunctionReturnsObject();

ClassType const &x = ret;

, thus it's easy to see how the "lifetime is extended".

I think many people have suggested tho that C++ _should_ allow the
following:

void Func(ClassType &x);

ClassType FunctionReturnsObject(void);

int main(void)
{
Func(FunctionReturnsObject());
}

--
Tomás Ó hÉilidhe
Dec 14 '07 #2
Hi George,

The function "B source() { return A(); }" create a temporary of type
B, and a a temporary is a rvalue.
according to the C++ standard a rvalue cannot be bound to a non-const
reference, i.e. B(B&){} cannot be called since it take a non-const
reference. Instead, the temporary B is converted to A again using
"operator A()" and passed to the B(A){}.

Hope it helps,

Iftekhar
// C4350.cpp
// compile with: /W1
#pragma warning (default : 4350)
class A {};

class B
{
public:
B(B&){}
// try the following instead
// B(const B&){}

B(A){}
operator A(){ return A();}
};

B source() { return A(); }

int main()
{
B ap(source()); // C4350
}
On Dec 14, 7:17 am, George2 <george4acade...@yahoo.comwrote:
Hello everyone,

I am debugging MSDN code from,

http://msdn2.microsoft.com/en-us/lib...ah(VS.80).aspx

here is my output,

1>main.cpp
1>d:\visual studio 2008\projects\test_c4350\test_c4350\main.cpp(21) :
warning C4350: behavior change: 'B::B(A)' called instead of 'B::B(B
&)'
1 d:\visual studio 2008\projects
\test_c4350\test_c4350\main.cpp(13) : see declaration of 'B::B'
1 d:\visual studio 2008\projects
\test_c4350\test_c4350\main.cpp(9) : see declaration of 'B::B'
1 A non-const reference may only be bound to an lvalue

I do not quite understand why there is a warning A non-const reference
may only be bound to an lvalue and 'B::B(A)' called instead of 'B::B(B
&)'? Could anyone give some descriptions please?

thanks in advance,
George
Dec 14 '07 #3
On Dec 14, 7:17 am, George2 <george4acade...@yahoo.comwrote:
I am debugging MSDN code from,
http://msdn2.microsoft.com/en-us/lib...ah(VS.80).aspx
Note that the example is designed expressedly to provoke this
error.
here is my output,
1>main.cpp
1>d:\visual studio 2008\projects\test_c4350\test_c4350\main.cpp(21) :
warning C4350: behavior change: 'B::B(A)' called instead of 'B::B(B
&)'
1 d:\visual studio 2008\projects
\test_c4350\test_c4350\main.cpp(13) : see declaration of 'B::B'
1 d:\visual studio 2008\projects
\test_c4350\test_c4350\main.cpp(9) : see declaration of 'B::B'
1 A non-const reference may only be bound to an lvalue
I do not quite understand why there is a warning A non-const reference
may only be bound to an lvalue and 'B::B(A)' called instead of 'B::B(B
&)'?
What don't you understand about it? What is provoking it? Or
why it is an error? (It's forbidden by the C++ standard. The
rule was introduced some time around 1990, or a little before,
because practical experience showed too many errors resulting
from an implicit conversion binding to a non-const reference.)

At the beginning, many compilers only generated a warning, to
give people time to modify their code, but today, practically
all compilers treat it as an error. (Except Microsoft, I
think.)
Could anyone give some descriptions please?
Of what? The rule. The rationale behind it? Or is there some
term or expression in it that you don't understand.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Dec 14 '07 #4

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

Similar topics

12
4398
by: lothar | last post by:
re: 4.2.1 Regular Expression Syntax http://docs.python.org/lib/re-syntax.html *?, +?, ?? Adding "?" after the qualifier makes it perform the match in non-greedy or minimal fashion; as few...
3
12190
by: Mario | last post by:
Hello, I couldn't find a solution to the following problem (tried google and dejanews), maybe I'm using the wrong keywords? Is there a way to open a file (a linux fifo pipe actually) in...
25
7550
by: Yves Glodt | last post by:
Hello, if I do this: for row in sqlsth: ________pkcolumns.append(row.strip()) ________etc without a prior:
32
4469
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
22
12001
by: Steve - DND | last post by:
We're currently doing some tests to determine the performance of static vs non-static functions, and we're coming up with some odd(in our opinion) results. We used a very simple setup. One class...
2
6091
by: Ian825 | last post by:
I need help writing a function for a program that is based upon the various operations of a matrix and I keep getting a "non-aggregate type" error. My guess is that I need to dereference my...
0
2321
by: amitvps | last post by:
Secure Socket Layer is very important and useful for any web application but it brings some problems too with itself. Handling navigation between secure and non-secure pages is one of the cumbersome...
399
12638
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post by:
PEP 1 specifies that PEP authors need to collect feedback from the community. As the author of PEP 3131, I'd like to encourage comments to the PEP included below, either here (comp.lang.python), or...
9
3790
by: Francois Grieu | last post by:
When running the following code under MinGW, I get realloc(p,0) returned NULL Is that a non-conformance? TIA, Francois Grieu #include <stdio.h> #include <stdlib.h>
12
29800
by: puzzlecracker | last post by:
is it even possible or/and there is a better alternative to accept input in a nonblocking manner?
0
7267
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
7391
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
7553
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...
1
7120
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7542
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...
0
4754
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3247
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3235
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
809
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.