Boost logo

Boost :

From: Valentin Bonnard (Bonnard.V_at_[hidden])
Date: 1999-08-31 11:50:05


Greg Colvin wrote:
>
> From: Valentin Bonnard <Bonnard.V_at_[hidden]>
> > Greg Colvin wrote:
> >
> > > From: Valentin Bonnard <Bonnard.V_at_[hidden]>
> > > > ...
> > > > I propose the following:
> > > >
> > > > // precondition: that debug_mode is defined
> > > > #define BOOST_ASSERT_THROW(condition) \
> > > > (debug_mode ? boost::Utils::assert_throw(condition, __LINE__,
> > > > __FILE__) : 0)
> > > >
> > > > namespace boost {
> > > > namespace Utils {
> > > > struct AssertFailure : std::logic_error {
> > > > AssertFailure (long line, const char* file);
> > > > ...
> > > > };
> > > >
> > > > inline void assert_throw (bool c, long line, const char* file)
> > > > { if (c) throw AssertFailure (line, file); }
> > > > }
> > > > using namespace Utils;
> > > > }
> > >
> > > So any user of Boost must define exactly one extern debug_mode in
> > > order to link?
> >
> > Yes. They must declare it in every TU which uses my macro
> > before uses of the macro.
>
> Which means in every translation unit that includes any Boost header?

If Boost uses this macro in headers (not just .cc files), then
yes.

> > There is no no-checks-by-default behaviour (there is no
> > default behaviour at all).
> >
> > They can use an static constant but it isn't recommanded.
> > They can use an auto variable.
> > > And being a link-time constant, the code for the condition cannot
> > > be compiled away?
> >
> > You do it as you want: compile time constant, ``link time''
> > constant, or even a variable. It's up to you.
> >
> > I recommend a compile-time constant with external linkage.
>
> You mean in each translation unit you would put something like this?
>
> const debug_mode = MY_DEBUG;

(const bool I guess)

I recommended a constant with *external* linkage.

> Doesn't that break the ODR? Or am I confused again?

If MY_DEBUG is defined the same way in all headers, it's
fine. If not, _and_ you are using headers which use the
macro, then it breaks the ODR.

I leave the responsabillity to define debug_mode
consistently to the user.

Yes, it's pretty easy to break the ODR by defining an
constant with external linkage with different values
in different TUes. At least, in this case, the bug is
with the definition of debug_mode, not with the macro.

-- 
Valentin Bonnard

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