Boost logo

Boost :

From: Beman Dawes (beman_at_[hidden])
Date: 2000-07-24 16:07:18


John Maddock wrote:

>Last time this was discussed, the best candidate that Steve and I came up
>with was something like the form given below, usage is very simple, and
>since it only generates a typedef, it can be used anywhere (class,
function
>or namespace scope) without generating any code or data:

Steve also pointed me to the technique in private email.

We have been trying for a long time to come up with a pure C++ compile time
assert and this seems the best yet, so I won't object because of the
macros. It doesn't fail silently if misused the way some of the other
suggestions did. It meets the other requirements such as generating no
code or data. It works for a bunch of compilers (I tested w/Borland,
Metrowerks, and Microsoft).

Could you and/or Steve pull it and a bit of documentation together and
formally submit it?

>namespace boost{
>
>template <bool> struct compile_time_assert;
>
>template <> struct compile_time_assert<true>{};
>
>template<int> struct ct_assert_test{};
>
>}
>
>#define BOOST_CT_ASSERT( B ) typedef
>::boost::ct_assert_test<sizeof(::boost::compile_time_assert< ( B ) >)>
>BOOST_JOIN(_boost_postulate_details_,__LINE__)

If I understand it correctly, the indirection provided by ct_assert_test is
required to force instantiation of compile_time_assert. If
compile_time_assert were typedefed directly, the compiler would not
instantiate the typedef. Or something like that... Anyhow, it would be
nice to document why it is necessary.

Thanks for stepping forward,

--Beman
  


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