|
Boost : |
From: Robert Ramey (ramey_at_[hidden])
Date: 2003-06-17 01:34:01
John Maddok wrote:
>Another problem (I think) is that with BOOST_STATIC_ASSERT you know compilation will stop.
>However, a BOOST_STATIC_WARNING would keep re-apearing in each translation unit #including your file:
>// boost_file.hpp
>BOOST_STATIC_WARNING( "we're in boost_file.hpp")
>// file1.hpp
>#include <boost/boost_file.hpp>
>-- appears here
>// file2.hpp
>#include <boost/boost_file.hpp>
a) BOOST_STATIC_WARNING only appears once since include files are guarded by
#ifdef ....
#define
...
#endif
BOOST_STATIC_ASSERT doesn't stop compilation. It only appears once for the
same reason that BOOST_STATIC_WARNING does.
Here is my best effort at BOOST_STATIC_WARNING.
It suffers from the followng deficiencies:
a) On a Microsoft platform it only works within a function definition.
b) It gives a warning that points to the exact line, BUT doesn't have a particularly eye catching message
Other than that, it seems to work as one would hope.
Robert Ramey
// (C) Copyright Robert Ramey 2003.
// Permission to copy, use, modify, sell and
// distribute this software is granted provided this copyright notice appears
// in all copies. This software is provided "as is" without express or implied
// warranty, and with no claim as to its suitability for any purpose.
// See http://www.boost.org/libs/static_assert for documentation.
/*
Revision history:
15 June 2003
Initial version.
*/
#ifndef BOOST_STATIC_WARNING_HPP
#define BOOST_STATIC_WARNING_HPP
#include <boost/config.hpp>
namespace boost {
// used to generate warning
template<const char *T> struct WARNING_MESSAGE;
template<bool>
class STATIC_WARNING;
template<>
class STATIC_WARNING<false>
{
typedef char type;
};
template<>
class STATIC_WARNING<true>
{
typedef int type;
};
} // boost
// Implemenation
// this implementation relies on the warning issued by compilers
// when a constant is truncated. If the boolean is false, the
// compiler emits a warning message. The warning message points
// to the point where the macro was invoked. The warning message
// indicates data lost in an integral conversion. Unfortunately,
// we have yet to discover an way to invoke a more eye catching
// warning message a la BOOST_STATIC_ASSERT.
#if defined(BOOST_MSVC)
// __LINE__ macro broken when -ZI is used see Q199057
// Sooo for this compiler this only works inside a function
#define BOOST_STATIC_WARNING(B) \
{static_cast< ::boost::STATIC_WARNING< (bool)(B) >::type >(0xffff);}
#else
#define BOOST_STATIC_WARNING(B) \
enum { \
BOOST_JOIN(boost_static_warning_enum_, __LINE__) \
= static_cast< ::boost::STATIC_WARNING< (bool)(B) >::type >(0xffff) \
}
#endif
#endif // BOOST_STATIC_WARNING_HPP
/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
// test_static_warning.cpp:
// (C) Copyright 2002 Robert Ramey - http://www.rrsd.com . Permission to copy,
// use, modify, sell and distribute this software is granted provided this
// copyright notice appears in all copies. This software is provided "as is"
// without express or implied warranty, and with no claim as to its suitability
// for any purpose.
#include <boost/static_warning.hpp>
#if ! defined(BOOST_MSVC)
BOOST_STATIC_WARNING(false);
BOOST_STATIC_WARNING(true);
#endif
int
test_main( int argc, char* argv[] )
{
BOOST_STATIC_WARNING(true);
BOOST_STATIC_WARNING(false);
return 0;
}
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk