Boost logo

Boost :

From: Beman Dawes (beman_at_[hidden])
Date: 1999-08-30 13:18:58

Greg Colvin wrote:

>From: Beman Dawes <beman_at_[hidden]>
>> In private email, Kevlin Henney points out that use of assert() in
>> boost headers violates the ODR (one-definition rule).
>How so? Could you please elaborate?

NDEBUG can be set differently in different compilation units. That
causes an ODR violation if assert() is used by definitions
(presumably in headers). Quoting Bjarne, "This implies that assert()
shouldn't be used in inline functions and template functions that are
included in several translation units unless great care is taken than
NDEBUG is set consistently."

>I recall some discussion of assert and the ODR some years ago, and
>suggestions, I think from Andy Koenig, on how to fix it, but it
>happen, and I know longer remember the details. I also recall that
>Vilot tried, and failed, to introduce a template-based alternative.

I don't remember the details either, but I think Mike's approach may
have generated some code even when not active, and people wouldn't
buy that. Any attempts to fix it ran afoul of the ODR. But I think
the approach I suggested avoids those two problems.

>> ...
>> Do others think that some such template base assertion approach is
>> worth developing for boost? Has anyone else tried to develop
>> assert() replacements?
>There are four things I like about assert:
>1) Being a macro, it can report the location in the source where it

I think that is less of an issue now that most debuggers take you to
the point of failure automatically, with the call stack available.

>2) Being a macro, it it easy to redefine it to throw, log, or

Yes, but we are back to trying to spell "undefined behavior"?

>3) Being standard, it is always available.
>4) Being standard, I have no qualms insisting that team members use
>We can fix 3 and 4 some years out, and a Boost template might be a
>in that direction, but 1 and 2 are harder to pull off.
>Regardless, I would hate to do anything that might discourage
>from using assert in C++ code.

If you rephrase that as "I would hate to do anything that might
discourage programmers from using assertions in C++ code" then I
certainly agree with you. The question is "what form should those
assertions" take? Providing an alternative to assert() doesn't stop
programmers from using assert() where they prefer it to the

Sooner or later compilers are going to start diagnosing ODR
violations. At that point, people like me who have used assert() in
headers are going to have a problem, I think.


Boost list run by bdawes at, gregod at, cpdaniel at, john at