Menu

#83 clarify constant-assertion example and add a rationale (i.e., using local func)

1.0.0
accepted
None
Documentation
minor
0.4.1
enhancement
2012-09-10
2012-09-10
No

=Constant assertions =

I would expect that the old are always const so the following

postcondition(
auto old_even = CONTRACT_OLDOF even,
auto old_odd = CONTRACT_OLDOF odd,
// [old_]even and [old_]odd all const& within
assertions.
const( even, old_even ) even == old_even + 2,
const( odd, old_odd ) odd == old_odd + 2
)

old_... are const but you need to specify them in const( ... ) so they
can be accessed by the constant-assertion boolean condition:

const( var1, var2, ... ) boolean-condition-using-var1-var2-...

Why?

Because constant-assertions are implemented using local functions
which have such a limitation. Essentially, constant-assertions are
local function's constant blocks:
http://www.boost.org/doc/libs/1_51_0/libs/local_function/doc/html/boost_localfunction/examples.html#boost_localfunction.examples.constant_blocks
I'll document this rationale.

Discussion

  • Lorenzo Caminiti

    • status changed from new to accepted
     
  • Lorenzo Caminiti

    Also clarify that reversing order of operator== arguments is a good practice but not good enough in general:

    the arguments in assertions/checks is a good practice:

    old_odd + 2 == odd

    Sure. In general assertions might call f(old_odd, odd) where f's
    arguments are non-const references but for easy checks like == the
    above is a good practice as usual (e.g., that's how I usually write
    if-conditions).

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.