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); }; template <> struct foo<int> { foo (int i) { } }; int main () { foo<int> f (7); } // end example program I get the following error: foo.cpp(8): error: incomplete type is not allowed BOOST_STATIC_ASSERT (false); ^ compilation aborted for foo.cpp (code 2) Whether I provide the specialization for int or not, or whether I ever use struct foo in main or not. Is this a know issue? Do I have a build problem? I'm using boost version 1.30.2. Thanks Scott
Thomas S. Urban wrote:
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); }; <snip>
There is a known problem with compilers that implement standard two-phase name resolution in templates. See http://lists.boost.org/MailArchives/boost/msg06980.php.
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.
On Thu, Oct 14, 2004 at 11:20:25, John Maddock spake thusly: ...
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.
Thanks, this works fine on the two I'm using.
participants (3)
-
Ben Hutchings
-
John Maddock
-
Thomas S. Urban