Modifying mutable data via const references.

Discussion in 'C++' started by jason.cipriani@gmail.com, Feb 21, 2008.

  1. Guest

    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
     
    , Feb 21, 2008
    #1
    1. Advertising

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

  3. 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
     
    Andrey Tarasevich, Feb 22, 2008
    #3
  4. * Andrey Tarasevich:
    > 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?
     
    Alf P. Steinbach, Feb 22, 2008
    #4
  5. Guest

    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" <> 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
     
    , Feb 22, 2008
    #5
  6. Guest

    On Feb 22, 12:13 am, ""
    <> 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
     
    , Feb 22, 2008
    #6
  7. 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
     
    Andrey Tarasevich, Feb 22, 2008
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Ballard
    Replies:
    6
    Views:
    406
    Peter Hansen
    May 27, 2004
  2. Michael Chermside
    Replies:
    1
    Views:
    343
    Roy Smith
    May 26, 2004
  3. Javier
    Replies:
    2
    Views:
    622
    James Kanze
    Sep 4, 2007
  4. Francesco
    Replies:
    0
    Views:
    419
    Francesco
    Aug 31, 2009
  5. fdm
    Replies:
    11
    Views:
    817
    Francesco
    Sep 1, 2009
Loading...

Share This Page