Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Modifying mutable data via const references.

Reply
Thread Tools

Modifying mutable data via const references.

 
 
jason.cipriani@gmail.com
Guest
Posts: n/a
 
      02-21-2008
Hello,

Recently, in this newsgroup, I learned that if you bind an object to a
const reference, like this:

SomeObject a;
const SomeObject &ca = a;

And also like this:

void function (const SomeObject &ca);
SomeObject a;
function(a);

The compiler is completely free to create a copy of the object and
pass a reference to that copy instead, if it wants. I may be wrong
about this. However, if I'm not, does that mean that the following
code produces undefined results:

struct SomeObject {
mutable int n;
};

void elsewhere (void) {
SomeObject s;
const SomeObject &c = s;
c.n = 3; // <--- is it possible this doesn't change s.n?
}

void function (const SomeObject &s) {
s.n = 3; // <--- same deal here.
}

Will modifying mutable members of objects through const references not
necessarily modify that member in the object you assigned the
reference to (since the compiler may have created a copy, and you are
modifying the copy instead)?

What about something like this (same definition of SomeObject as
above) (in general, I know I am leaving a lot out of this snippet):

std::set<SomeObject>::const_iterator ci = SomeObjects.find(SomeKey);
(*ci).n = 3;

Is it possible that that code would not change the 'n' member of the
object in the set?

I also learned recently that the compiler is free to create a copy of
an object when casting away it's const, such as when using
const_cast<> or a C-style cast. Does that mean that the following is
undefined (a contrived example, I know):

struct SomeObject {
int n;
void SetNTo3 (void) { n = 3; }
};

void function (const SomeObject &c) {
(const_cast<SomeObject &>(c)).SetNTo3();
}

Is it possible that the const_cast will have caused a copy of 'c' to
be created, and so SetNTo3() applies to a different, temporary
instance?

Thanks,
Jason
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      02-21-2008
* http://www.velocityreviews.com/forums/(E-Mail Removed):
> Hello,
>
> Recently, in this newsgroup, I learned that if you bind an object to a
> const reference, like this:
>
> SomeObject a;
> const SomeObject &ca = a;
>
> And also like this:
>
> void function (const SomeObject &ca);
> SomeObject a;
> function(a);
>
> The compiler is completely free to create a copy of the object and
> pass a reference to that copy instead, if it wants


No, but in the current standard it can do that if you bind the reference
to an rvalue.

This possibility has been removed in the C++0x draft.

Cheers, & hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
 
 
 
Andrey Tarasevich
Guest
Posts: n/a
 
      02-22-2008
(E-Mail Removed) wrote:
> Hello,
>
> Recently, in this newsgroup, I learned that if you bind an object to a
> const reference, like this:
>
> SomeObject a;
> const SomeObject &ca = a;
>
> And also like this:
>
> void function (const SomeObject &ca);
> SomeObject a;
> function(a);
>
> The compiler is completely free to create a copy of the object and
> pass a reference to that copy instead, if it wants. I may be wrong
> about this.


You are wrong. Apparently, you misunderstood something. In this specific case
the language specification strictly requires that the reference is bound
directly to the initializer lvalue object. No copies can be created.

> What about something like this (same definition of SomeObject as
> above) (in general, I know I am leaving a lot out of this snippet):
>
> std::set<SomeObject>::const_iterator ci = SomeObjects.find(SomeKey);
> (*ci).n = 3;
>
> Is it possible that that code would not change the 'n' member of the
> object in the set?


I'm not sure that the behavior is defined in this case, but it really has
nothing to do with the above issue if reference binding. It's more about
standard container specification.

> I also learned recently that the compiler is free to create a copy of
> an object when casting away it's const, such as when using
> const_cast<> or a C-style cast. Does that mean that the following is
> undefined (a contrived example, I know):
>
> struct SomeObject {
> int n;
> void SetNTo3 (void) { n = 3; }
> };
>
> void function (const SomeObject &c) {
> (const_cast<SomeObject &>(c)).SetNTo3();
> }
>
> Is it possible that the const_cast will have caused a copy of 'c' to
> be created, and so SetNTo3() applies to a different, temporary
> instance?
> ...


No, it is not. The result will always refer to the same object.

--
Best regards,
Andrey Tarasevich
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      02-22-2008
* Andrey Tarasevich:
> (E-Mail Removed) wrote:
>> Hello,
>>
>> Recently, in this newsgroup, I learned that if you bind an object to a
>> const reference, like this:
>>
>> SomeObject a;
>> const SomeObject &ca = a;
>>
>> And also like this:
>>
>> void function (const SomeObject &ca);
>> SomeObject a;
>> function(a);
>>
>> The compiler is completely free to create a copy of the object and
>> pass a reference to that copy instead, if it wants. I may be wrong
>> about this.

>
> You are wrong. Apparently, you misunderstood something. In this specific
> case the language specification strictly requires that the reference is
> bound directly to the initializer lvalue object. No copies can be created.


Earlier, <url:
http://groups.google.com/group/comp.lang.c++/msg/d6991be959ae9b7b>, I
replied the following (essentially same as your paragraph)

<quote>
No, but in the current standard it can do that if you bind the reference
to an rvalue.

This possibility has been removed in the C++0x draft.
</quote>

But that article does not exist not on my news-server (it's my ISPs news
server, and they in turn use SuperNews as provider of this service).

It seems there is something very odd and very wrong going on with news
propagation, and has been going on for some months. And it seems it's
not a SuperNews / provider-specific problem. E.g., earlier I checked
some clc++m articles that were missing (although present in Google's
archive) -- regularly, only about half of clc++m articles show up on
my news-server -- and it turned out they had not been propagated to
the University of Oslo, either.

When you replied, were you aware of my earlier article?


Cheers,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
jason.cipriani@gmail.com
Guest
Posts: n/a
 
      02-22-2008
Thanks for your replies, guys. It got me started -- I am muddling
through the standard trying to get a handle on it before I come back
with any more questions.

On Feb 21, 11:49 pm, "Alf P. Steinbach" <(E-Mail Removed)> wrote:
> Earlier, <url:
> http://groups.google.com/group/comp.lang.c++/msg/d6991be959ae9b7b>, I
> replied the following (essentially same as your paragraph)

[snip]
> When you replied, were you aware of my earlier article?


I use Google Groups to read the newsgroup sometimes, and Outlook
Express with some free usenet server other times (not SuperNews).
FWIW, that article didn't appear on the free usenet server, and while
it appears in Google Groups in your link, it's not associated with
this thread. I wasn't aware it existed until you posted that link.

I have been having a lot of problems with Google Groups recently.
Mostly posts that I make via Google Groups that do not appear on the
news server, even though they appear in the groups.

I have also noticed a lot of broken threads recently, "Re: etc..."
without any sign of the original messages. I am not sure where the
problem is. I do know Google Groups gets a little weird sometimes. I
had an annoying experience recently posting on borland.public.*
newsgroups (which are served on news.borland.com but are not Usenet
groups). Even though they aren't Usenet groups they appear as Google
Groups, and if you post to them via Google Groups your post appears on
Google with no errors. Except because they aren't Usenet, they don't
receive the posts you make on Google. And there's no way of knowing
what happened without checking the Borland newsgroup server as well.

It's pretty annoying.

But anyways, again, thanks for your replies. I misunderstood something
that I read before, apparently. I will go over it again and see if I
can make it make sense.

Jason
 
Reply With Quote
 
jason.cipriani@gmail.com
Guest
Posts: n/a
 
      02-22-2008
On Feb 22, 12:13 am, "(E-Mail Removed)"
<(E-Mail Removed)> wrote:
> FWIW, that article didn't appear on the free usenet server, and while
> it appears in Google Groups in your link, it's not associated with
> this thread. I wasn't aware it existed until you posted that link.


No, this is wrong. I misread the link you posted as different than the
message you posted in this thread, then jumped to a conclusion because
of the other newsgroup problems I've been having recently. It appeared
and looked just fine.

In general, though, I didn't make up the rest of the stuff I said. I
have been having lots of problems just like that, especially related
to Google Groups, and getting gradually worse over the last few months
(I do not remember having these kinds of issues, say, a year ago).

Jason
 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      02-22-2008
Alf P. Steinbach wrote:
> ...
> When you replied, were you aware of my earlier article?
> ...


Yes, I could see it. It was visible on Giganews server as well as on
aioe.cjb.net server.

--
Best regards,
Andrey Tarasevich
 
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
Modifying non-const object from const function? fdm C++ 11 09-01-2009 03:14 PM
Re: Modifying non-const object from const function? Francesco C++ 0 08-31-2009 11:30 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
modifying mutable list elements in a for loop Peter Ballard Python 6 05-27-2004 01:36 AM
RE: modifying mutable list elements in a for loop Michael Chermside Python 1 05-26-2004 02:17 PM



Advertisments