mutable v's non const

Discussion in 'C++' started by Paul, Jul 9, 2011.

  1. Paul

    Paul Guest

    Hi groupies.

    What is the meaning of the "mutable" word in C++ circles?
    Mutable apparently means "it can change". So why are the terms "mutable" and
    "non-mutable" used instead of const and non-const and in what way are they
    different?
     
    Paul, Jul 9, 2011
    #1
    1. Advertising

  2. Paul

    red floyd Guest

    On 7/9/2011 10:15 AM, Paul wrote:
    > Hi groupies.
    >
    > What is the meaning of the "mutable" word in C++ circles?
    > Mutable apparently means "it can change". So why are the terms "mutable"
    > and "non-mutable" used instead of const and non-const and in what way
    > are they different?


    A mutable class member may be modified even if the object is const.

    e.g.:

    class Example {
    public:
    void good() const {
    mutable_int = 7;
    }
    void bad() const {
    non_mutable_int = 7; // ERROR
    }
    Example() :
    mutable_int(0), non_mutable_int(0)
    {
    }
    private:
    mutable int mutable_int;
    int non_mutable_int;
    };

    Example::good() will compile, and is fine.
    Example::bad() will not compile because it's modifying a
    non-mutable member of a const object.
     
    red floyd, Jul 10, 2011
    #2
    1. Advertising

  3. Paul

    Paul Guest

    "red floyd" <> wrote in message
    news:ivb9mo$grv$...
    > On 7/9/2011 10:15 AM, Paul wrote:
    >> Hi groupies.
    >>
    >> What is the meaning of the "mutable" word in C++ circles?
    >> Mutable apparently means "it can change". So why are the terms "mutable"
    >> and "non-mutable" used instead of const and non-const and in what way
    >> are they different?

    >
    > A mutable class member may be modified even if the object is const.
    >
    > e.g.:
    >
    > class Example {
    > public:
    > void good() const {
    > mutable_int = 7;
    > }
    > void bad() const {
    > non_mutable_int = 7; // ERROR
    > }
    > Example() :
    > mutable_int(0), non_mutable_int(0)
    > {
    > }
    > private:
    > mutable int mutable_int;
    > int non_mutable_int;
    > };
    >

    Thanks for the example.

    Why would you make such a function const in the first place?
    If its not const why mark it as const?
    > Example::good() will compile, and is fine.
    > Example::bad() will not compile because it's modifying a
    > non-mutable member of a const object.
    >
    >
     
    Paul, Jul 10, 2011
    #3
  4. Paul

    Ian Collins Guest

    On 07/10/11 08:58 PM, Paul wrote:
    >
    > "red floyd"<> wrote in message
    > news:ivb9mo$grv$...
    >> On 7/9/2011 10:15 AM, Paul wrote:
    >>> Hi groupies.
    >>>
    >>> What is the meaning of the "mutable" word in C++ circles?
    >>> Mutable apparently means "it can change". So why are the terms "mutable"
    >>> and "non-mutable" used instead of const and non-const and in what way
    >>> are they different?

    >>
    >> A mutable class member may be modified even if the object is const.
    >>
    >> e.g.:
    >>
    >> class Example {
    >> public:
    >> void good() const {
    >> mutable_int = 7;
    >> }
    >> void bad() const {
    >> non_mutable_int = 7; // ERROR
    >> }
    >> Example() :
    >> mutable_int(0), non_mutable_int(0)
    >> {
    >> }
    >> private:
    >> mutable int mutable_int;
    >> int non_mutable_int;
    >> };
    >>

    > Thanks for the example.
    >
    > Why would you make such a function const in the first place?
    > If its not const why mark it as const?


    So it can be called on a const instance of an Example.

    --
    Ian Collins
     
    Ian Collins, Jul 10, 2011
    #4
  5. Paul

    Daniel Weber Guest

    Am 10.07.2011 10:58, schrieb Paul:
    > Why would you make such a function const in the first place?
    > If its not const why mark it as const?


    Consider the difference between technically const and semantically const.

    You might e.g. have a class (the program's configuration settings) which
    needs to synchronize parallel calls to some/all methods and therefore
    has a helper attribute which is used for the synchronization. When one
    of the synchronized method is semantically const - it doesn't change any
    of the configuration settings - but it has to change the helper attribute...

    Bye,
    Daniel
     
    Daniel Weber, Jul 10, 2011
    #5
  6. Paul

    Miles Bader Guest

    Leigh Johnston <> writes:
    >> Consider the difference between technically const and semantically const.

    >
    > I think the terms "logically const" and "physically const" are more
    > cromulent.


    "consty" ?

    -miles

    --
    We live, as we dream -- alone....
     
    Miles Bader, Jul 10, 2011
    #6
  7. Paul

    Paul Guest

    "Daniel Weber" <> wrote in message
    news:ivbvl5$oj2$...
    > Am 10.07.2011 10:58, schrieb Paul:
    >> Why would you make such a function const in the first place?
    >> If its not const why mark it as const?

    >
    > Consider the difference between technically const and semantically const.
    >
    > You might e.g. have a class (the program's configuration settings) which
    > needs to synchronize parallel calls to some/all methods and therefore has
    > a helper attribute which is used for the synchronization. When one of the
    > synchronized method is semantically const - it doesn't change any of the
    > configuration settings - but it has to change the helper attribute...
    >

    It seems to be a bit of a cheat to allow a modifiable data member of a const
    object.
    I did a little research , it seems to be a case that this only applies to
    non static members. Handy for debugging according to the first couple of
    pages I read about it.

    After further thinking about your explanation about a helper attribute it
    seems pretty much spot on.
     
    Paul, Jul 10, 2011
    #7
  8. Paul

    Paul Guest

    "Leigh Johnston" <> wrote in message
    news:...
    > On 10/07/2011 11:41, Daniel Weber wrote:
    >> Am 10.07.2011 10:58, schrieb Paul:
    >>> Why would you make such a function const in the first place?
    >>> If its not const why mark it as const?

    >>
    >> Consider the difference between technically const and semantically const.

    >
    > I think the terms "logically const" and "physically const" are more
    > cromulent.
    >

    Daniels terms seem more fitting to me. I did see the term "logically
    constant" in one of the articles I read to be fair to you.
     
    Paul, Jul 10, 2011
    #8
  9. Paul

    Paul Guest

    "Paul" <> wrote in message
    news:OjjSp.1595$2...
    >
    > "Daniel Weber" <> wrote in message
    > news:ivbvl5$oj2$...
    >> Am 10.07.2011 10:58, schrieb Paul:
    >>> Why would you make such a function const in the first place?
    >>> If its not const why mark it as const?

    >>
    >> Consider the difference between technically const and semantically const.
    >>
    >> You might e.g. have a class (the program's configuration settings) which
    >> needs to synchronize parallel calls to some/all methods and therefore has
    >> a helper attribute which is used for the synchronization. When one of the
    >> synchronized method is semantically const - it doesn't change any of the
    >> configuration settings - but it has to change the helper attribute...
    >>

    > It seems to be a bit of a cheat to allow a modifiable data member of a
    > const object.
    > I did a little research , it seems to be a case that this only applies to
    > non static members.


    Maybe I am incorrect about this only being applicable to nonstaic data
    members. Can anyone clarify if the mutable qualifier can be used on objects
    other than nonstatic class members?
     
    Paul, Jul 10, 2011
    #9
  10. Paul

    Paul Guest

    "Werner Erasmus" <> wrote in message
    news:...
    > Paul wrote:
    >
    >> Maybe I am incorrect about this only being applicable to nonstaic data
    >> members. Can anyone clarify if the mutable qualifier can be used on
    >> objects other than nonstatic class members?

    >
    > What would be the point of making them applicable to static
    > class members? The keyword mutable only makes sense during
    > the execution of const (member) functions. Par 7.1.1:10 (n3242)
    > makes it clear that mutable only applies to names of class data
    > members, and not to members declared const or static. 7.1.1:11
    > elaborates on its precise purpose.
    >


    I guess I was just wondering if this sort of thing was possible:

    struct X{ const int i;}
    mutable X anX;
    anX.i =5;
     
    Paul, Jul 10, 2011
    #10
  11. Paul

    Paul Guest

    "Leigh Johnston" <> wrote in message
    news:...
    > On 10/07/2011 21:45, Paul wrote:
    >>
    >> "Werner Erasmus" <> wrote in message
    >> news:...
    >>> Paul wrote:
    >>>
    >>>> Maybe I am incorrect about this only being applicable to nonstaic data
    >>>> members. Can anyone clarify if the mutable qualifier can be used on
    >>>> objects other than nonstatic class members?
    >>>
    >>> What would be the point of making them applicable to static
    >>> class members? The keyword mutable only makes sense during
    >>> the execution of const (member) functions. Par 7.1.1:10 (n3242)
    >>> makes it clear that mutable only applies to names of class data
    >>> members, and not to members declared const or static. 7.1.1:11
    >>> elaborates on its precise purpose.
    >>>

    >>
    >> I guess I was just wondering if this sort of thing was possible:
    >>
    >> struct X{ const int i;}
    >> mutable X anX;
    >> anX.i =5;

    >
    > FFS: TIAS; or to put it another way stop wasting bandwidth (and people's
    > time) on Usenet.
    >

    If you think it's a waste of bandwidth then why add to the bandwidth wastage
    with your plop?
     
    Paul, Jul 10, 2011
    #11
  12. Paul

    Paul Guest

    "Paul" <> wrote in message
    news:hcoSp.17567$2...
    >
    > "Werner Erasmus" <> wrote in message
    > news:...
    >> Paul wrote:
    >>
    >>> Maybe I am incorrect about this only being applicable to nonstaic data
    >>> members. Can anyone clarify if the mutable qualifier can be used on
    >>> objects other than nonstatic class members?

    >>
    >> What would be the point of making them applicable to static
    >> class members? The keyword mutable only makes sense during
    >> the execution of const (member) functions. Par 7.1.1:10 (n3242)
    >> makes it clear that mutable only applies to names of class data
    >> members, and not to members declared const or static. 7.1.1:11
    >> elaborates on its precise purpose.
    >>

    >
    > I guess I was just wondering if this sort of thing was possible:
    >
    > struct X{ const int i;}
    > mutable X anX;
    > anX.i =5;
    >

    .....or if there were any situations where mutable could be applied other
    than a nonstatic data member.

    Please ignore Leighs rude comments as he has a tendancy to be nasty
    especially when he bears a grudge or three ;-)

    Anyway thanks for posting of the quote from the standard as that seems to
    comfirm my original assumption.
     
    Paul, Jul 11, 2011
    #12
  13. Paul

    Paul Guest

    "Leigh Johnston" <> wrote in message
    news:...
    > On 11/07/2011 17:43, Paul wrote:
    > [snip]
    >
    >>
    >> Please ignore Leighs rude comments as he has a tendancy to be nasty
    >> especially when he bears a grudge or three ;-)
    >>

    >
    > No grudges; you're just an arsehole.
    >

    And what have I done to you to deserve that?
    All I have done is prove you wrong about things on a few occasions for which
    you seem to bear a grudge. <shrug>
     
    Paul, Jul 11, 2011
    #13
  14. Paul

    Paul Guest

    "Leigh Johnston" <> wrote in message
    news:...
    > On 11/07/2011 18:04, Paul wrote:
    >>
    >> "Leigh Johnston" <> wrote in message
    >> news:...
    >>> On 11/07/2011 17:43, Paul wrote:
    >>> [snip]
    >>>
    >>>>
    >>>> Please ignore Leighs rude comments as he has a tendancy to be nasty
    >>>> especially when he bears a grudge or three ;-)
    >>>>
    >>>
    >>> No grudges; you're just an arsehole.
    >>>

    >> And what have I done to you to deserve that?
    >> All I have done is prove you wrong about things on a few occasions for
    >> which you seem to bear a grudge. <shrug>

    >
    > You are an arsehole because, for example, you constantly claim to have
    > proven others wrong when you haven't (just like you did just now).

    I have proved you wrong on many occassions , you just do not accept it.

    > You are also actively homophobic which also counts towards you being an
    > arsehole.

    :-S
    WTF does homophobia have to do with anything and so what if I was
    homophobic. I'd rather be homophobic than homosexual.



    > You are also an arsehole because you are unable to take criticism, admit
    > to gaps in your knowledge and apologize for your mistakes.
    >


    Not at all. That's more like a description of yourself.
     
    Paul, Jul 11, 2011
    #14
  15. Paul <> wrote:
    > Why would you make such a function const in the first place?
    > If its not const why mark it as const?


    A practical example where you need to modify the mutable member variable
    of a const object is a reference-counting smart pointer which uses double
    linking for the reference counting.

    Such a smart pointer, instead of actually using an actual integral
    reference count, has a 'prev' and 'next' pointer as members, like a
    doubly-linked list. Whenever two smart pointers point to the same object
    (in which case the "reference count" would be larger than 1), they are
    linked together with those pointers (possibly via a larger chain of
    smart pointer objects, if there are more than two pointing to the same
    object). The advantage of this is that you don't need to allocate a
    separate reference count (which is more efficient), nor require one to
    be in the managed object (which means the smart pointer is not intrusive
    and thus works with any type). The disadvantage is that the size of the
    smart pointer is that of three pointers.

    The 'prev' and 'next' pointers have to be mutable. There's no way around
    it. That's because the copy constructor and assignment operators must take
    the parameter as const:

    TehSmartPointer::TehSmartPointer(const TehSmartPointer& rhs):
    object(rhs.object)
    {
    // Insert 'this' into the linked list where 'rhs' is. It has to modify
    // 'rhs.prev' and 'rhs.prev->next', which have to be mutable.
    }
     
    Juha Nieminen, Jul 12, 2011
    #15
  16. On Jul 11, 10:32 am, "Paul" <> wrote:
    > "Leigh Johnston" <> wrote in message
    >
    > news:...
    >
    >
    >
    > > On 11/07/2011 18:04, Paul wrote:

    >
    > >> "Leigh Johnston" <> wrote in message
    > >>news:...
    > >>> On 11/07/2011 17:43, Paul wrote:
    > >>> [snip]

    >
    > >>>> Please ignore Leighs rude comments as he has a tendancy to be nasty
    > >>>> especially when he bears a grudge or three ;-)

    >
    > >>> No grudges; you're just an arsehole.

    >
    > >> And what have I done to you to deserve that?
    > >> All I have done is prove you wrong about things on a few occasions for
    > >> which you seem to bear a grudge. <shrug>

    >
    > > You are an arsehole because, for example, you constantly claim to have
    > > proven others wrong when you haven't (just like you did just now).

    >
    > I have proved you wrong on many occassions , you just do not accept it.
    >
    > >  You are also actively homophobic which also counts towards you beingan
    > > arsehole.

    >
    > :-S
    > WTF does homophobia have to do with anything and so what if I was
    > homophobic. I'd rather be homophobic than homosexual.


    Paul, please post only when you have something on-topic to contribute.
     
    Oliver Jackson, Jul 12, 2011
    #16
  17. Paul

    MikeP Guest

    > On 7/9/2011 10:15 AM, Paul wrote:

    > A mutable class member may be modified even if the object is const.


    A mutable data member of a class may be modified by a const member
    function of the class.
     
    MikeP, Jul 15, 2011
    #17
    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. wtnt
    Replies:
    1
    Views:
    684
    Andrey Tarasevich
    May 7, 2004
  2. pit3k
    Replies:
    2
    Views:
    1,017
    sadhu
    Feb 8, 2005
  3. Javier
    Replies:
    2
    Views:
    606
    James Kanze
    Sep 4, 2007
  4. Replies:
    17
    Views:
    1,693
    James Kanze
    Oct 5, 2007
  5. fungus
    Replies:
    13
    Views:
    930
    fungus
    Oct 31, 2008
Loading...

Share This Page