Path: utzoo!attcan!uunet!mcvax!ukc!dcl-cs!aber-cs!pcg From: pcg@aber-cs.UUCP (Piercarlo Grandi) Newsgroups: comp.lang.c++ Subject: Re: A C++ Macro Package implementing *Assertions* (was: Re: Eiffel vs. C++) Summary: You *can* do entry-exit assertions; in g++ even class assertions... Message-ID: <1000@aber-cs.UUCP> Date: 9 Jun 89 14:14:50 GMT Reply-To: pcg@cs.aber.ac.uk (Piercarlo Grandi) Organization: Dept of CS, UCW Aberystwyth (Disclaimer: my statements are purely personal) Lines: 40 In article <764@cirrusl.UUCP> paul@sun600 (Paul E. Black) writes: Here's our version of an "assert" (run-time check) macro. [ ... ] #ifdef RIGOROUS #define ASSERT(ex) {if (!(ex)) (void)fprintf(stderr,"ex not true in %s, line %d\n",__FILE__,__LINE__);} #else #define ASSERT(ex) #endif This is a bit lame... :-). If you create a class for assertions, with constructors and destructors, you can have them automatically checked at block entry and exit; general idea is: { ENSURE(entry-condition,exit-condition); .... } where ENSURE expands to an immediate check for entry-condition, and wraps up exit-condition in some destructor of a dummy object. With G++, if you use wrappers, you can even have some contrived form of class invariants. In general constructors and destructors in C++ allow you to define almost fully general entry-exit functions (I posted some months ago two small classes, for clean exception handling and tracing, that use this aspect of the language). In practice the only limit is the non easy problem of non local dynamic control transfers; in theory destructors ought to be called whenever the current block instance is passivated and constructors whenever it is activated. (in the general case, not in C++ yet (:->), you can reenter and leave a block *instance* many times). Notably, setjmp/longjmp does ignore destructors... This is a well known problem; BS supposedly is thinking about macaroni and catch/throw and continuations ... :-> -- Piercarlo "Peter" Grandi | ARPA: pcg%cs.aber.ac.uk@nsfnet-relay.ac.uk Dept of CS, UCW Aberystwyth | UUCP: ...!mcvax!ukc!aber-cs!pcg Penglais, Aberystwyth SY23 3BZ, UK | INET: pcg@cs.aber.ac.uk