Boost logo

Boost :

From: Beman Dawes (beman_at_[hidden])
Date: 1999-09-01 10:34:02


Valentin Bonnard wrote:

>Beman Dawes wrote:
>
>> Looking at 7.3.1.1 [namespace.unnamed], it seems the following
>> technique allows the use of assert() in a header without
introducing
>> an ODR violation.
>>
>> namespace boost {
>> namespace {
>> template <class Derived, class Base>
>> inline Derived polymorphic_cast(Base* x) {
>> assert( dynamic_cast<Derived>(x) != 0 ); // detect logic
>> error
>> return static_cast<Derived>(x);
>> }
>> }
>> }
>>
>> If this doesn't work,
>
>Technically this code is fine.
>
>But look at the problem from the user point of view:
>it means that he can only use boost' casts in functions
>with no or static linkage, or functions defined only
>once.

In other words, the problem has been reduced from the template always
being an ODR violation if there were NDEBUG differences between
translations units to only sometimes, depending on usage, being an
ODR violation. Better, but not good enough.

>Your code is correct, but puts pressure on the user.
>Boost should try to simplify (when possible) the
>user's life.

It is looking like we have to remove the asserts from the
throw_on_error versions of the casts.

But it isn't quite that simple for the non-throwing versions, like
polymorphic_cast<> above. Is it worth keeping? Does it become too
dangerous? The whole point was to provide debug mode error checking
where none previously existed.

--Beman


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