Boost logo

Boost :

From: Johan Paulsson (boost.org_at_[hidden])
Date: 2006-11-06 07:44:13


Below is the compile-time assert I use.
Would it be a useful addition to Boost?

I know Boost has compile-time assertions, but I think this is different:

-----------

template<bool if_> struct
  assertion
   { enum { assert }; };
template<> struct
  assertion<false>
   {};

template<int if_, int assert_ = assertion<if_ != 0>::assert> struct
  assert {};

template<typename if_, int assert_ = if_::assert> struct
  type_assert {};

// they are used via typedef:

typedef assert<false> assert_1;
typedef type_assert<assertion<false> > assert_2;

// type_assert can make nicer error messages:

template<typename type_> struct
  not_integer_type :
   assertion<
     std::numeric_limits<type_>::is_integer
> {};

typedef type_assert<not_integer_type<float> > assert_3;

--------------

Because assertion<false>::assert does not exist, the default template
argument to assert is bad when if_ == 0. The "assert_1" and "assert_2"
typedefs should make the compiler say something similar to:

file.cpp line 123: error: 'assert' is not a member of 'assertion<false>'

The "assert_3" typedef should make the compiler say something similar to:

file.cpp line 123: error: 'assert' is not a member of
'not_integer_type<float>'

It works on the compilers I have:
- CodeWarrior 10.0
- GCC 3.2, 3.3, 3.4, 4.0, 4.1
- Intel C++ 9.0
- Visual C++ 7.1, 8.0

Regards,
Johan Paulsson


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