Boost logo

Boost Users :

From: John Maddock (john_at_[hidden])
Date: 2004-10-14 05:20:25


> The BOOST_STATIC_ASSERT documentation indicates I can use it at template
> class scope (private recommended) - the assert shouldn't get triggered
> unless the class is instantiated. This seems to work ok with gcc-3.*
> but not with the intel compiler (8.1).
>
> // begin example program
> #include "boost/static_assert.hpp"
>
> template<class T> struct foo
> {
> foo (int) { }
> private:
> BOOST_STATIC_ASSERT (false);
> };

This is a recurring problem, and one for which I should update the
documentation: if the contents of the assert are not dependent upon a
template parameter, then a "sufficiently clever" compiler can evaluate the
assert at the point it is declared, realise that there is no instantiation
of that template that could ever be legal, and reject the code. Normally
this is a "good thing", but unfortunately messes up static asserts as you
have found. If you really want an assert that always fails then try:

template<class T> struct foo
{
foo (int) { }
private:
BOOST_STATIC_ASSERT (sizeof(T) == 0);
};

although there is still some question as to whether a "sufficiently clever"
compiler could regard this as ill formed as well :-( Fortunately none do at
present.

John.


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