Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Reg. Doubt in C++ References

Reply
Thread Tools

Reg. Doubt in C++ References

 
 
VSP
Guest
Posts: n/a
 
      01-15-2007
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







 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      01-15-2007
VSP wrote:

> 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
 
Reply With Quote
 
 
 
 
Mehturt@gmail.com
Guest
Posts: n/a
 
      01-15-2007

> 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

 
Reply With Quote
 
Zorro
Guest
Posts: n/a
 
      01-15-2007

Kai-Uwe Bux wrote:
> VSP wrote:
>
> > 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


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,

http://www.zhmicro.com
http://distributed-software.blogspot.com
http://groups-beta.google.com/group/...Z?lnk=la&hl=en

 
Reply With Quote
 
Dizzy
Guest
Posts: n/a
 
      01-15-2007
Zorro wrote:

> 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.

--
Dizzy
http://dizzy.roedu.net

 
Reply With Quote
 
Zorro
Guest
Posts: n/a
 
      01-16-2007

Dizzy wrote:
> Zorro wrote:
>
> > 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.
>
> --
> Dizzy
> http://dizzy.roedu.net


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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dotnet doubt can any body clarify my doubt challa462@gmail.com ASP .Net 0 08-22-2012 06:02 AM
doubt about doubt Bob Nelson C Programming 11 07-30-2006 08:17 PM
Difference between bin and obj directories and difference between project references and dll references jakk ASP .Net 4 03-22-2005 09:23 PM
doubt regarding Symbolic references Rajesh Perl Misc 3 03-08-2005 09:16 PM
Pointers and References (and References to Pointers) Roger Leigh C++ 8 11-17-2003 10:14 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57