-5

I came Across a line of code in an example that I can not understand how is it being utilized and what benefit on using this type of declaration. following in the part of code extracted from that example

const uint16_t cycles = 8;
typedef uint8_t invariant(value < cycles) TIndex_t;

struct TData
{
uint16_t readings[cycles];
volatile uint16_t sum;
TIndex_t index;

void addReading(uint16_t arg)
writes(*this; volatile)
pre(invar())
pre(arg <= 1023)
post(invar())
{
   sum = sum - readings[index] + arg;
   readings[index] = arg;
   index = static_cast<TIndex_t>((index + 1u) % cycles);
}
void init()
writes(*this; volatile)
post(invar());

ghost(
    bool invar() const
        returns((forall r in readings :- r <= 1023) && sum == + over readings);
)

This is GNU C++ the struct is a kind of class definition as I can tell

1 Answers1

1

This is not pure C++.

It looks like somebody has written either a compiler extension or a series of macros to allow the specification of pre- and post-conditions for a block of code.

You're going to have to ask that person.

(As an aside, this is why just "extracting code" is often insufficient for understanding; you need to observe the full context in order to know what's going on. In this case, the context includes definitions and/or documentation found elsewhere and not included in your question.)

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055