|
Boost : |
From: Gregory Seidman (gseidman_at_[hidden])
Date: 2001-01-23 11:34:42
Dean Sturtevant sez:
} --- In boost_at_[hidden], Gregory Seidman <gseidman_at_a...> wrote:
} > Why not just use #ifndef NDEBUG? It keeps things consistent with
} > cassert and, like cassert, allows automatic building of debugging and
} > optimized versions. Of course, with any system we devise it is possible
} > for the end programmer to put the #ifndef NDEBUG in his own code, but
} > much of the point of boost is to make less work for the programmer.
} >
} > What is the win of a scheme other than NDEBUG?
} >
} As with anything, there are tradeoffs. If it was up to me, I'd do
} what you suggested. But when I introduced the idea of putting asserts
} in boost (last month?), some people objected on the basis that
} the 'one definition rule' (ODR) would be violated. They were
} concerned that modules compiled with different settings of NDEBUG
} might be linked together in the same program. In order to accommodate
} them, I proposed using a different namespace (boost_debug) to put
} assert-laden code in. This has the drawback that one then needs a
} macro to establish the boost namespace, but it does avoid ODR
} violations. In fact, this is the technique the STLPort uses.
Okay, try this on for size. We have two namespaces: boost_debug and
boost_optimized (or whatever... bear with me). Each boost header looks
something like this:
#ifndef BOOST_THISFUNCTIONALITY_HPP
#define BOOST_THISFUNCTIONALITY_HPP
#ifndef NDEBUG
//the code in this file is contained in namespace boost_debug;
#include <boost/thisfunctionality_debug.hpp>
#else
//the code in this file is contained in namespace boost_optimized
#include <boost/thisfunctionality_optimized.hpp>
#endif
namespace boost {
#ifndef NDEBUG
using namespace boost_debug;
#else
using namespace boost_optimized;
#endif
}
#endif //BOOST_THISFUNCTIONALITY_HPP
Now, the debug stuff is in a separate namespace from the ordinary or
optimized version, so linking between debugging and non-debugging objects
will fail outright. Nonetheless, the programmer simply uses boost:: (or
using namespace boost) to access whichever version (which will be drawn
into the boost namespace), but the version is chosen at compile-time with
(or without) -DNDEBUG. Note that the includes in the middle could just be
the appropriate definitions in the appropriate namespace; I showed it as
includes for simplicity and readability.
We preserve ODR while preserving ease of use. Any objections?
} - Dean
--Greg
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk