typedef int (*fpt)() [[ensures r: r != 0]]; // error: contract condition not on a function declaration int g(int x) [[expects: x >= 0]] [[ensures r: r > x]] { return x+1; } int (*pf)(int) = g; // OK int x = pf(5); // contract conditions of g are checked— end example
class X {
public:
int v() const;
void f() [[expects: x > 0]]; // error: x is private
void g() [[expects: v() > 0]]; // OK
friend void r(int z) [[expects: z > 0]]; // OK
friend void s(int z) [[expects: z > x]]; // error: x is private
protected:
int w();
void h() [[expects: x > 0]]; // error: x is private
void i() [[ensures: y > 0]]; // OK
void j() [[ensures: w() > 0]]; // OK
int y;
private:
void k() [[expects: x > 0]]; // OK
int x;
};
class Y : public X {
public:
void a() [[expects: v() > 0]]; // OK
void b() [[ensures: w() > 0]]; // error: w is protected
protected:
void c() [[expects: w() > 0]]; // OK
}; — end examplevoid f(int * p) [[expects: p != nullptr]] // #1 [[ensures: *p == 1]] // #3 [[expects: *p == 0]] // #2 { *p = 1; }— end example
int f(int x)
[[ensures r: r == x]]
{
return ++x; // undefined behavior
}
int g(int * p)
[[ensures r: p != nullptr]]
{
*p = 42; // OK, p is not modified
}
int h(int x)
[[ensures r: r == x]]
{
potentially_modify(x); // undefined behavior if x is modified
return x;
} — end example