Modifying mutable data via const references.

Discussion in 'C++' started by, Feb 21, 2008.

  1. 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;

    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

, Feb 21, 2008
    1. Advertisements

  2. * :
    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
    Alf P. Steinbach, Feb 21, 2008
    1. Advertisements

  3. 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.
    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.
    No, it is not. The result will always refer to the same object.
    Andrey Tarasevich, Feb 22, 2008
  4. * Andrey Tarasevich:
    Earlier, <url:>, I
    replied the following (essentially same as your paragraph)

    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.

    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?


    - Alf
    Alf P. Steinbach, Feb 22, 2008
  5. 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.

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

, Feb 22, 2008
  6. 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).

, Feb 22, 2008
  7. Yes, I could see it. It was visible on Giganews server as well as on server.
    Andrey Tarasevich, Feb 22, 2008
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.