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 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
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
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
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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;
|
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...
|
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...
|
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...
|
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....
|
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
|
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...
|
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)\...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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,...
| |