|
Boost : |
From: Gregory Seidman (gseidman_at_[hidden])
Date: 2001-01-23 16:06:29
Dean Sturtevant sez:
} I have objections.
} Much of debug functionality would be implemented by using asserts.
} These asserts should go in the code one uses even in non-debug
} versions. Thus I don't see much use for a separate header file for
} the debug code.
I'm confused. Much of the point of assert() is that it vanishes when
compiling with -DNDEBUG. Why should the asserts be in the non-debug code?
Are you just saying that the source code of debug version should be
identical to the non-debug version and that duplication of the code is the
problem? That can be fixed with the following:
namespace
#ifndef NDEBUG
boost_debug
#else
boost_optimized
#endif
{
//code goes here
}
//and again the same thing I had in the previous message
namespace boost {
#ifndef NDEBUG
using namespace boost_debug;
#else
using namespace boost_optimized;
#endif
}
} Also, this means that one cannot have something like:
}
} namespace boost
} {
} template <class Type>
} class Array
} {
} ...
} Type& operator[](unsigned index){
} assert(index < size());
} return m_elements[index];
} }
} ...
} };
} }
}
} because that would violate the ODR (boost::Array<Type>::operator[]()
} would have a different definition for NDEBUG).
}
} Instead one would need to say:
} #ifndef NDEBUG
} #define BOOST_NAMESPACE boost_debug
} namespace boost_debug {}
} namespace boost = boost_debug;
} #else
} #define BOOST_NAMESPACE boost
} #endif
}
} namespace BOOST_NAMESPACE
} {
} ...
} }
...which is equivalent to what I wrote above (I think; I don't recall if
namespace foo = bar; is equivalent to namespace foo { using namespace bar; },
but I think so). What is the problem with that?
--Greg
} --- In boost_at_[hidden], Gregory Seidman <gseidman_at_a...> wrote:
} > 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?
} >
} > --Greg
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk