Reg. Doubt in C++ References

V

VSP

Hi,

I have a doubt regarding using references.
Please look at the below code. I am using VC++ 6.0

int &i = 10; // Compilation error

error C2440: 'initializing' : cannot convert from 'const int' to 'int &'
A reference that is not to 'const' cannot be bound to a non-lvalue

Assume that "Test" is a class
Test &t = Test(); // No compilation error

Why for built-in types assigning non-const reference with a non-lvalue is
giving error and for the
User defined type it is not giving any error?

Thanks
VSP
 
K

Kai-Uwe Bux

VSP said:
I have a doubt regarding using references.
Please look at the below code. I am using VC++ 6.0

int &i = 10; // Compilation error

error C2440: 'initializing' : cannot convert from 'const int' to 'int &'
A reference that is not to 'const' cannot be bound to a non-lvalue

Assume that "Test" is a class
Test &t = Test(); // No compilation error

Why for built-in types assigning non-const reference with a non-lvalue is
giving error and for the
User defined type it is not giving any error?

According to the standard, you should see an error in both cases.
Apparently, your compiler is broken. Try:

struct Test {};

int main ( void ) {
Test & t = Test();
}


at: http://www.comeaucomputing.com/tryitout/



Best

Kai-Uwe Bux
 
M

Mehturt

I have a doubt regarding using references.
Please look at the below code. I am using VC++ 6.0

int &i = 10; // Compilation error

error C2440: 'initializing' : cannot convert from 'const int' to 'int &'
A reference that is not to 'const' cannot be bound to a non-lvalue

Assume that "Test" is a class
Test &t = Test(); // No compilation error

Why for built-in types assigning non-const reference with a non-lvalue is
giving error and for the
User defined type it is not giving any error?

Your compiler is broken.
Try your sample on Comeau online -
http://www.comeaucomputing.com/tryitout
m
 
Z

Zorro

Kai-Uwe Bux said:
According to the standard, you should see an error in both cases.
Apparently, your compiler is broken. Try:

struct Test {};

int main ( void ) {
Test & t = Test();
}


at: http://www.comeaucomputing.com/tryitout/



Best

Kai-Uwe Bux

Let us look at this from a different angle, before dismissing it. Note
that, at "Test()" an object is in fact created, within the same scope
as the reference to it. So, within that scope, t can indeed reach the
object. This is not in voilation of object-oriented view, though it may
violate the standard.
However, C++ takes the C built-in types as is. Then, the literal 10 is
not an object (not an l-value for C). There is nothing to point to
because a literal for a built-in is only available during compilation
unless it is assigned to an object (of its type).

Regards,
(e-mail address removed)
http://www.zhmicro.com
http://distributed-software.blogspot.com
http://groups-beta.google.com/group/computerlangZ?lnk=la&hl=en
 
D

Dizzy

Zorro said:
Let us look at this from a different angle, before dismissing it. Note
that, at "Test()" an object is in fact created, within the same scope
as the reference to it. So, within that scope, t can indeed reach the
object. This is not in voilation of object-oriented view, though it may
violate the standard.

So you wonder what is the reasoning behind that decision in the standard ?
However, C++ takes the C built-in types as is. Then, the literal 10 is
not an object (not an l-value for C). There is nothing to point to
because a literal for a built-in is only available during compilation
unless it is assigned to an object (of its type).

But for references to const C++ allows to bind to a literal like that.

int const& i = 10;
(the compiler will create a temporary in the same scope)

It's someway unified that references to const are allowed to temporaries
even to literals like that and it makes programming typeless interfaces (ie
templates) easier.
 
Z

Zorro

Dizzy said:
So you wonder what is the reasoning behind that decision in the standard ?


But for references to const C++ allows to bind to a literal like that.

int const& i = 10;
(the compiler will create a temporary in the same scope)

It's someway unified that references to const are allowed to temporaries
even to literals like that and it makes programming typeless interfaces (ie
templates) easier.

The compiler error already indicated that it could do it for const. How
is const related to what I said?
I am aware of C++ templates accepting literals like "10", instead of
types. That is because C++ template instantiation is done the same way
C macros are, i.e. text editing. I am not objecting to this, just
saying how it works, and that is the way one should accept it.

Please note that I was explaining why VC++ did not generate error for
Test(). That is because, Test() is an object (after elaboration). There
is no const involved in my response.

Thanks for your comment, though.
Regards.
 

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

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top