Re: definition for invariant and class invariant

Discussion in 'C++' started by Jonathan Lee, Jul 30, 2010.

  1. Jonathan Lee

    Jonathan Lee Guest

    On Jul 29, 11:09 pm, ", India"
    <> wrote:
    > In the above program, (from the above definition, I thought an
    > 'invariant' is a non-static const data member and a 'class invariant'
    > is a static const data member)


    While const data members may fit the literal definition of
    "invariant",
    usually it is used to describe a sort of rule that a section of code
    or class must obey.

    For example, I expect the std::vector class has a class invariant
    "the size of this vector is less than or equal to the reserved
    size". Operations must then obey this invariant. If the vector were to
    add new elements, it must ensure that there is enough reserved data
    to hold those new elements. And if the vector's reserve were to shrink
    (as some kind of garbage collection, for instance), then it can only
    shrink a certain amount.

    There are more formal ways of saying this, but that's the general
    idea.

    Take a look here, too: http://en.wikipedia.org/wiki/Object_invariant

    --Jonathan
     
    Jonathan Lee, Jul 30, 2010
    #1
    1. Advertising

  2. Jonathan Lee

    James Kanze Guest

    On Jul 30, 6:27 am, Jonathan Lee <> wrote:
    > On Jul 29, 11:09 pm, ", India"


    > <> wrote:
    > > In the above program, (from the above definition, I thought an
    > > 'invariant' is a non-static const data member and a 'class invariant'
    > > is a static const data member)


    > While const data members may fit the literal definition of
    > "invariant",


    Not really, since an invariant must be a predicate. (A const
    data member of type bool could be considered an invariant, but
    it's not a very interesting one, since it is trivially true,
    regardless of what you do.)

    --
    James Kanze
     
    James Kanze, Jul 30, 2010
    #2
    1. Advertising

  3. Jonathan Lee

    Jonathan Lee Guest

    On Jul 30, 6:35 am, James Kanze <> wrote:
    > On Jul 30, 6:27 am, Jonathan Lee <> wrote:
    >
    > > On Jul 29, 11:09 pm, ", India"
    > > <> wrote:
    > > > In the above program, (from the above definition, I thought an
    > > > 'invariant' is a non-static const data member and a 'class invariant'
    > > > is a static const data member)

    > > While const data members may fit the literal definition of
    > > "invariant",

    >
    > Not really, since an invariant must be a predicate.  (A const
    > data member of type bool could be considered an invariant, but
    > it's not a very interesting one, since it is trivially true,
    > regardless of what you do.)
    >
    > --
    > James Kanze


    "const" is essentially the predicate P(x) = "x does not change".
    A const data member is a value such that P(x) is true. So, okay,
    the value _itself_ isn't a predicate, but it immediately implies
    one.

    --Jonathan
     
    Jonathan Lee, Jul 30, 2010
    #3
  4. Jonathan Lee

    James Kanze Guest

    On Jul 30, 2:51 pm, Jonathan Lee <> wrote:
    > On Jul 30, 6:35 am, James Kanze <> wrote:
    > > On Jul 30, 6:27 am, Jonathan Lee <> wrote:


    > > > On Jul 29, 11:09 pm, ", India"
    > > > <> wrote:
    > > > > In the above program, (from the above definition, I thought an
    > > > > 'invariant' is a non-static const data member and a 'class invariant'
    > > > > is a static const data member)
    > > > While const data members may fit the literal definition of
    > > > "invariant",


    > > Not really, since an invariant must be a predicate. (A const
    > > data member of type bool could be considered an invariant, but
    > > it's not a very interesting one, since it is trivially true,
    > > regardless of what you do.)


    > "const" is essentially the predicate P(x) = "x does not change".


    One can look at it like that, although the predicate would be
    more along the lines "x == whatever".

    A predicate involving time doesn't make sense as an invariant,
    since it is the concept of invariant which introduces the
    element of time: at all instances (within a certain time frame),
    the predicate is true.

    > A const data member is a value such that P(x) is true. So, okay,
    > the value _itself_ isn't a predicate, but it immediately implies
    > one.


    A const data member certainly implies a predicate: "x ==
    initialization value", and thus an invariant. Such invariants
    are usually not considered, however, since they are evident and
    impossible (more or less) to violate. More interesting are
    invariants like the one cited by someone else, "size() <=
    capacity()" for std::vector, or loop invariants like "i >= 0 &&
    i < v.size()" (where v is a vector).

    --
    James Kanze
     
    James Kanze, Jul 31, 2010
    #4
    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. DaKoadMunky

    Handling class invariant violations

    DaKoadMunky, Dec 4, 2004, in forum: C++
    Replies:
    8
    Views:
    1,480
    Victor Bazarov
    Dec 5, 2004
  2. Jianli Shen
    Replies:
    1
    Views:
    606
    Victor Bazarov
    Mar 13, 2005
  3. Giovanni Bajo

    min/max: "stable" invariant?

    Giovanni Bajo, Mar 11, 2008, in forum: Python
    Replies:
    1
    Views:
    255
    Terry Reedy
    Mar 11, 2008
  4. helveticus

    Keeping datetime culture invariant

    helveticus, Sep 22, 2008, in forum: ASP .Net
    Replies:
    2
    Views:
    438
    helveticus
    Sep 23, 2008
  5. James Kanze
    Replies:
    0
    Views:
    455
    James Kanze
    Jul 30, 2010
Loading...

Share This Page