Boost logo

Boost Users :

From: Ricardo Muñoz (rmunoz_at_[hidden])
Date: 2008-03-18 16:19:49


On Tue, Mar 18, 2008 at 6:31 PM, John Maddock <john_at_[hidden]> wrote:
> Jean-Francois Bastien wrote:
> >> We have this really neat and simple macro in our project:
> >> #define compile_assert(x) typedef bool
> >> COMPILE_ASSERT[(x) ? 1 : -1]
> >>
> >> Although it does what we need and serves us well, I
> >> tried to replace
> >> it with BOOST_STATIC_ASSERT. Unfortunately this resulted
> >> in a lot of
> >> ugly warnings throughout the compile:
> >> warning: use of old-style cast
> >>
> >
> > There's the following comment in the static assert header:
> >
> > // Note that the argument to the assert is explicitly cast to bool
> > using old-
> > // style casts: too many compilers currently have problems with
> > static_cast
> > // when used inside integral constant expressions.
> >
> > The solution would be to add even more defines to use old style or
> > static casts depending on which compiler is used.
>
> Right: historically this is why the header doesn't use static_cast here.
>
> Unfortunately GCC appears to be one of the compilers that chokes if you use
> static_cast in an integral constant expression: testing the diff below with
> gcc-3.4 (cygwin) results in the tests failing (where as enabling the same
> static_cast useage with msvc or intel does work OK).
>
> If you anyone can test this with gcc-4.x and let me know which versions
> might accept this patch OK that would be very useful, I'm attaching the
> updated static_assert.hpp as well as the diff.

Hi!, hope this help:

ricardo_at_miles:~/devel$ cat foo_static.cpp
#include <boost/static_assert.hpp>

BOOST_STATIC_ASSERT(sizeof(int) >= sizeof(short));

int main()
{
  return 0;
}
ricardo_at_miles:~/devel$ svn info boost-trunk/
Path: boost-trunk
URL: http://svn.boost.org/svn/boost/trunk
Repository Root: http://svn.boost.org/svn/boost
Repository UUID: b8fc166d-592f-0410-95f2-cb63ce0dd405
Revision: 43710
Node Kind: directory
Schedule: normal
Last Changed Author: bemandawes
Last Changed Rev: 43709
Last Changed Date: 2008-03-18 20:35:18 +0100 (Tue, 18 Mar 2008)

ricardo_at_miles:~/devel$ gcc --version
gcc (GCC) 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

ricardo_at_miles:~/devel$ gcc foo_static.cpp -o foo_static -Wall
-Wold-style-cast -I./boost-trunk/
ricardo_at_miles:~/devel$

Regards,

> John.
>
> Here's the diff:
>
> Index: C:/data/boost/boost/trunk/boost/static_assert.hpp
> ===================================================================
> --- C:/data/boost/boost/trunk/boost/static_assert.hpp (revision 43667)
> +++ C:/data/boost/boost/trunk/boost/static_assert.hpp (working copy)
> @@ -28,6 +28,17 @@
> # define BOOST_SA_GCC_WORKAROUND
> #endif
>
> +//
> +// If the compiler supports the use of static_cast inside an integral
> +// constant expression, and it issues warnings about old C style casts,
> +// then enable this:
> +//
> +#if defined(__GNUC__) && ((__GNUC__ > 3) || ((__GNUC__ == 3) &&
> (__GNUC_MINOR__ >= 4)))
> +#define BOOST_STATIC_ASSERT_BOOL_CAST static_cast<bool>
> +#else
> +#define BOOST_STATIC_ASSERT_BOOL_CAST (bool)
> +#endif
> +
> #ifdef BOOST_HAS_STATIC_ASSERT
> # define BOOST_STATIC_ASSERT( B ) static_assert(B, #B)
> #else
> @@ -78,14 +89,14 @@
> #elif defined(BOOST_MSVC)
> #define BOOST_STATIC_ASSERT( B ) \
> typedef ::boost::static_assert_test<\
> - sizeof(::boost::STATIC_ASSERTION_FAILURE< (bool)( B ) >)>\
> + sizeof(::boost::STATIC_ASSERTION_FAILURE<
> BOOST_STATIC_ASSERT_BOOL_CAST ( B ) >)>\
> BOOST_JOIN(boost_static_assert_typedef_, __COUNTER__)
> #elif defined(BOOST_INTEL_CXX_VERSION) || defined(BOOST_SA_GCC_WORKAROUND)
> // agurt 15/sep/02: a special care is needed to force Intel C++ issue an
> error
> // instead of warning in case of failure
> # define BOOST_STATIC_ASSERT( B ) \
> typedef char BOOST_JOIN(boost_static_assert_typedef_, __LINE__) \
> - [ ::boost::STATIC_ASSERTION_FAILURE< (bool)( B ) >::value ]
> + [ ::boost::STATIC_ASSERTION_FAILURE<
> BOOST_STATIC_ASSERT_BOOL_CAST( B ) >::value ]
> #elif defined(__sgi)
> // special version for SGI MIPSpro compiler
> #define BOOST_STATIC_ASSERT( B ) \
> @@ -100,12 +111,12 @@
> #define BOOST_STATIC_ASSERT( B ) \
> BOOST_STATIC_CONSTANT(int, \
> BOOST_JOIN(boost_static_assert_test_, __LINE__) = \
> - sizeof(::boost::STATIC_ASSERTION_FAILURE< (bool)( B ) >) )
> + sizeof(::boost::STATIC_ASSERTION_FAILURE<
> BOOST_STATIC_ASSERT_BOOL_CAST( B ) >) )
> #else
> // generic version
> #define BOOST_STATIC_ASSERT( B ) \
> typedef ::boost::static_assert_test<\
> - sizeof(::boost::STATIC_ASSERTION_FAILURE< (bool)( B ) >)>\
> + sizeof(::boost::STATIC_ASSERTION_FAILURE<
> BOOST_STATIC_ASSERT_BOOL_CAST( B ) >)>\
> BOOST_JOIN(boost_static_assert_typedef_, __LINE__)
> #endif
>
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>

-- 
Ricardo Muñoz
Warp Networks S.L. http://www.warp.es

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net