Boost logo

Boost :

From: Dan W. (danw_at_[hidden])
Date: 2004-01-12 03:04:32

John Torjo wrote:
> But the thing is: why would we need something like this?
> Invariants are more like an idiom, and can be easily coded by hand, if
> one wants to (no need for any macros or anything).
> Or, you can create a certain function to access your internal data,
> which contains all preconditions, like this:
> const internal_data & get_data() const { check_invariant(); return
> m_data; }
> internal_data & get_data() { check_invariant(); return m_data; }
> And all code that uses internal data will have something like this at
> its beginning:
> internal_data & data = get_data();

Well, I'm lazy; if I can use 2 macros, rather than have to write
functons for each class, I will. (And I know programmers even lazier,
who will make data members public rather than type a get function...
then they forget about it...)
But notice also that it is actually more important to check invariants
at function exit; --i.e.: after (possibly) modifying the data, rather
than the next time you get around to read it. This way you catch the bug
with its paw holding a smoking gun. And since functions may have more
than one return, the logical way to achieve this is with an object on
the stack that verifies invariants on its ctor and dtor.

Anyways, it's hard to make Design by Contract immediately rewarding in
C++ as it is in Eiffel; but I'm hoping to at least make it a breeze. If
it takes a few macros, so be it.


Boost list run by bdawes at, gregod at, cpdaniel at, john at