Boost logo

Boost :

From: Csaba Szepesvari (szepes_at_[hidden])
Date: 1999-12-07 05:43:08


Dear All,

Attached is the ctassert solution I am using in my projects; it is slightly
different from the codes that appeared here.

Motivation (see also the documentation):

(R1) A minimal requirement of a good compile time assertion facility is to give
an error message *at the line* where the assertion fails.
(R2) Another requirement is that the facility should be usable in both
declarations and implementations in the same way.
(R3) The last requirement is that no real code should be generated and no
storage should be used.
(R4) No macros.
(R5) The namespaces should be polluted in a minimal way.

All the above requirements :-)
-- except --
the last two are supported by this implementation :-(

Unfortunately, in order to ensure R1 you need macros. In order to support R2 and
R3 you need class declarations. Unfortunately, this means some pollution of the
active scope. It also follows that the macro needs two parameters: the first
being the constant boolean expression to be evaluated and the second being the
"name of the check". These names should be different in the same scope for
repeated usage of this macro - in this respect the code could be improved
somewhat e.g. using __line__, etc.

Comments are appreciated.

- Csaba

Dave Abrahams wrote:

> Beman wrote:
> > One problem we ran into was that we wanted something which was not
> > subject to inadvertent misuse. For example, we had several
> > implementations of a "require" template class, which you could
> > inherit or instantiate:
> >
> > template<typename T>
> > class foo : require< std::numeric_limits<T>::is_integer > {...};
> >
> > The problem was that it could be misused:
> >
> > template<typename T>
> > class foo
> > {
> > require< std::numeric_limits<T>::is_integer >;
> > };
> >
> > This code looks right, but is actually totally ineffective (because
> > it should have been coded "require<
> > std::numeric_limits<T>::is_integer > some_name;").
> >
> > Any compile time assert should be bullet-proof, IMO.
>
> I'm not convinced:
> 1. compile-time assertions will be primarily for the advanced user and
> library programmer. The library's clients will get the benefit of the assert
> at compile time, when used properly
>
> 2. The problem you describe is no worse than many other pitfalls in the
> language; you have to know the language rules.
>
> e.g.
>
> class mutex;
> void foo()
> {
> mutex(); // Oops; does nothing!
>
> ...
> }
>
> 3. You could improve things by changing the name to require_t, making the
> error you describe less likely.
>
> -D
>
> ------------------------------------------------------------------------
> eGroups.com Home: http://www.egroups.com/group/boost/
> http://www.egroups.com - Simplifying group communications

--
=======================
Csaba Szepesvari
Head of Research Department, Mindmaker Ltd., Budapest 1121, HUNGARY
Konkoly Th. M. u. 29-33
WWW: http://victoria.mindmaker.hu/~szepes
e-mail: szepes_at_[hidden]
Tel: +36 1 3959220/1205
Fax: +36 1 3959218

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk