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 1694
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 explain how it achives the requirement ? I am
interested to know how auto_ptr_ref helps in transferring ownership and
how it avoids copying of const auto_ptrs.
|
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
with example
|
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 lifetime here means session time or not.
--------------------------------
From: sikander khan
-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)
<Id>A6zxS0rn8UyDgQKvmrzToA==</Id>
|
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. That means that stopped timers can not
be started again.'
| |
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
shared_ptr. one thread is meant to pass the shared_ptr to another after
its processing.
So when a message arrives i convert it into a class of
my own called 'Msg' and i put the 'Msg' object pointer into a
shared_ptr and put into the other thread's...
|
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: 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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |