Modifying mutable data via const references.

J

jason.cipriani

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
 
A

Alf P. Steinbach

* (e-mail address 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

Andrey Tarasevich

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.
 
A

Alf P. Steinbach

* Andrey Tarasevich:
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
 
J

jason.cipriani

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.

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
 
J

jason.cipriani

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
 
A

Andrey Tarasevich

Alf said:
...
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top