473,407 Members | 2,320 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Reg. Doubt in C++ References

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



Jan 15 '07 #1
5 1669
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

Jan 15 '07 #2
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
Jan 15 '07 #3

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,
zo****@ZHMicro.com
http://www.zhmicro.com
http://distributed-software.blogspot.com
http://groups-beta.google.com/group/...Z?lnk=la&hl=en

Jan 15 '07 #4
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

Jan 15 '07 #5

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.

Jan 16 '07 #6

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

Similar topics

7
by: Shan | last post by:
Hi all, In the following code the line *(int *)&i = 11; is confusing to me. What is it doing ? #include<iostream> using namespace std; int main() { const int i = 10; *(int *)&i = 11;
1
by: CID | last post by:
Hello all, I was reading about auto_ptr from TC++PL. It tells, the purpose of auto_ptr_ref is to implement the destructive copy semantics for ordinary auto_ptrs. Can somebody there please...
6
by: Baskar RajaSekharan | last post by:
In C-sharp, I wnat to know whether the Component is compiled in Debug Mode or Run Mode through Code. How is it possible? Is there any way to Access the Config file and check? Please let me know...
4
by: sikander khan via .NET 247 | last post by:
hello all ! i know that there exists one to one relationship between HttpApplication instance and a request. My doubt is that instance associated with that request until session times out. i mean...
2
by: archana | last post by:
Hi all, I am having one confusion regarding windows based timer. I read somewhere that 'Be careful with stopping, because stopped timers are disabled and are subject to garbage collection....
122
by: ivan | last post by:
hi all, if I have: if(A && B || C) which operation gets executed first? If I remeber well should be &&, am I correct? thanks
7
by: myfavdepo | last post by:
Hi all, I have a query regarding the exchanging of a boost::shared_ptr beween different threads. In my program i've two threads both of which having their own internal queues for storing the...
8
by: somenath | last post by:
Hi All, I have a doubt regarding the pointer assignment . Please have a look at the following program . #include<stdio.h> #include<stdlib.h> #define NAMESIZE 10 #define SAFE_FREE(t) if(t)\...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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
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...
0
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.