Hello all,
I've been wondering... Why is it that a reference may be bound only to a
const object? If a reference were bound to a non-const object and that
object were modified, what harm could result? A temporary is just as real
of an object as any other. It lacks a name, but that doesn't make it less
real. Class (no pun intended) warfare seems to be alive and well!
Thanks,
Dave 5 1536
Dave wrote: Hello all,
I've been wondering... Why is it that a reference may be bound only to a const object? If a reference were bound to a non-const object and that object were modified, what harm could result? A temporary is just as real of an object as any other. It lacks a name, but that doesn't make it less real. Class (no pun intended) warfare seems to be alive and well!
Thanks, Dave
Hm? What keeps you from doing this:
class A {};
A a;
A& ref = a;
// now work on ref (and therefore on a)
No const here.
"matthias_k " <no****@digital raid.com> wrote in message
news:ct******** *****@news.t-online.com... Dave wrote: Hello all,
I've been wondering... Why is it that a reference may be bound only to
a const object? If a reference were bound to a non-const object and that object were modified, what harm could result? A temporary is just as
real of an object as any other. It lacks a name, but that doesn't make it
less real. Class (no pun intended) warfare seems to be alive and well!
Thanks, Dave
Hm? What keeps you from doing this:
class A {};
A a; A& ref = a;
// now work on ref (and therefore on a)
No const here.
Sorry, my original post missed a key word - temporary. Why may only a
*const* reference be bound to a *temporary* object?
Put differently, what is the rationale for the rule that says I may not bind
a non-const reference to a temoirary object?
Dave wrote: "matthias_k " <no****@digital raid.com> wrote in message news:ct******** *****@news.t-online.com...
Dave wrote:
Hello all,
I've been wondering... Why is it that a reference may be bound only to a const object? If a reference were bound to a non-const object and that object were modified, what harm could result? A temporary is just as real of an object as any other. It lacks a name, but that doesn't make it less real. Class (no pun intended) warfare seems to be alive and well!
Thanks, Dave
Hm? What keeps you from doing this:
class A {};
A a; A& ref = a;
// now work on ref (and therefore on a)
No const here.
Sorry, my original post missed a key word - temporary. Why may only a *const* reference be bound to a *temporary* object? Put differently, what is the rationale for the rule that says I may not bind a non-const reference to a temoirary object?
If you could hold references to a (soon to be deleted) temporary, you
would be able to change the temporary through the alias name, although
this temporary may already be deleted. I guess this would be a situation
of undefined behavior.
Only allowing const references to reference a temporary keeps you from
doing such things.
Regards,
Matthias
> > Sorry, my original post missed a key word - temporary. Why may only a *const* reference be bound to a *temporary* object? Put differently, what is the rationale for the rule that says I may not
bind a non-const reference to a temoirary object?
If you could hold references to a (soon to be deleted) temporary, you would be able to change the temporary through the alias name, although this temporary may already be deleted. I guess this would be a situation of undefined behavior. Only allowing const references to reference a temporary keeps you from doing such things.
Regards, Matthias
However, the temporary persists until all references to it are gone. This
is already the case with a const reference being bound to a temporary.
In article <10************ *@news.supernew s.com>,
"Dave" <be***********@ yahoo.com> wrote: Sorry, my original post missed a key word - temporary. Why may only a *const* reference be bound to a *temporary* object? Put differently, what is the rationale for the rule that says I may not bind a non-const reference to a temoirary object?
If you could hold references to a (soon to be deleted) temporary, you would be able to change the temporary through the alias name, although this temporary may already be deleted. I guess this would be a situation of undefined behavior. Only allowing const references to reference a temporary keeps you from doing such things.
Regards, Matthias
However, the temporary persists until all references to it are gone. This is already the case with a const reference being bound to a temporary.
The motivation is spelled out in Stoustrup's excellent "The Design and
Evolution of C++", section 3.7. There's a good discussion, but it boils
down to this example:
void incr(int& rr) {rr++;}
void g()
{
double ss = 1;
incr(ss);
}
If temporaries could bind to non-const references then this program
would compile, but ss would not get incremented. By enforcing the
existing rule you turn a run time error into a compile time error (which
is always a Very Good Thing). You should really get the book for the
full story.
However, there is a move afoot to introduce a new reference type which
/will/ bind to temporaries. This characteristic, when combined with the
existing reference, can yield some extremely useful functionality. http://www.open-std.org/jtc1/sc22/wg...004/n1690.html
-Howard This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: kaede |
last post by:
Hi all,
I would like to know if the following code is valid and not
ill-formed.
Data getData()
{
return Data(1, 2, 3);
}
|
by: kaede |
last post by:
Hi all,
Consider the following code fragment:
// some data structure
class Data { ... }
// Container for the data structure
Class Container
{
|
by: JKop |
last post by:
Back when I read my first C++ book, I was given the following scenario:
class Cheese
{
public:
int number_of_holes;
int colour;
|
by: JKop |
last post by:
Firstly, we all know that temporaries are non-const, which I shall
demonstrate with the following code:
struct Blah
{
int monkey;
void SetMonkey(int const supplied)
{
monkey = supplied;
|
by: bipod.rafique |
last post by:
Hello All,
I need your help in understanding something.
I have a simple class
class test{
};
| |
by: ATASLO |
last post by:
In the following example, section #3 fails under VC98, VC2003, VC2005 Express
Beta (Aug 2004) and g++ 3.3.2. Is this just a pitfall of the C++
specification? Why don't any of the above compilers at least flag this as a
warning as they would when say trying to return a const & to a local?
In Section #2, the const B& Bref is initialized and bound to the temporary
returned from GetSettings(). That is the temporary B exists until Bref goes...
|
by: dragoncoder |
last post by:
Hi everyone, please consider the following function:-
const int& foo ( const double& d )
{
return d;
}
g++ compiles it with warnings and solaris CC gives error. I want to
know if the code is correct according to the standard ?
|
by: siddhu |
last post by:
Dear Experts,
I have a question.
I wrote the following code
class A
{
public:
void g(){}
|
by: Javier |
last post by:
Hello,
in which cases is it better the use of "const char*" to "string" (or
even const string &).
I mean, in STL (http://www.sgi.com/tech/stl/hash_map.html) I see:
hash_map<const char*, int, hash<const char*>, eqstrmonths;
months = 31;
I think it is for the use in calls like function("my string"), but, is
it really necessary to define funcion(const char*) besides
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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: 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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
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...
| |