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



 DEVELOP > c-Plus-Plus > A non-const reference may only be bound to an lvalue?

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "George2"
Date: 14 Dec 2007 12:17:23 AM
Object: 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/library/0eestyah(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
.

User: "Tomás Ó hÉilidhe"

Title: Re: A non-const reference may only be bound to an lvalue? 14 Dec 2007 03:38:59 AM
George2 <george4academic@yahoo.com> wrote 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
.

User: "iftekhar"

Title: Re: A non-const reference may only be bound to an lvalue? 14 Dec 2007 03:47:25 AM
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.com> wrote:

Hello everyone,

I am debugging MSDN code from,

http://msdn2.microsoft.com/en-us/library/0eestyah(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

.

User: "James Kanze"

Title: Re: A non-const reference may only be bound to an lvalue? 14 Dec 2007 06:44:03 AM
On Dec 14, 7:17 am, George2 <george4acade...@yahoo.com> wrote:

I am debugging MSDN code from,
http://msdn2.microsoft.com/en-us/library/0eestyah(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:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
.


  Page 1 of 1

1

 


Related Articles
how may in hell may i take advantage of a IF statement in two separate functions? like quit anytime with button Q
Yves Meynard's Enumeration class (CUJ Web exclusive, May 2003)
New POTM Contest: Deadline May 31, 2005
clean-up code before throwing an exception may also throw
Why being an indifferent typist may be harmful
May STL dtors throw?
which will be more efficient Stdlib MAP or Array /w sequential search for may be 50-1000 times?
windows service not starting, may be something to do with static variables
May I use a global data in a class?
Join Date: May 2006, Posts: 10 Reputation: HAOBBOY is an unknown quantity at this point (<10) Stopping Sorting Feature of Maps
Interview Questions May 1st, 2006
May I have a example of design pattern of "composite", I still feel fuzzy after reading book of Addison-Wesley's"design pattern "
problem about "default template arguments may not be used in function templates "
May a template argument be a friends of the template class?
Calling function that may throw an exception
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER