
BOOST_STATIC_ASSERT - On occasion I find it convenient to use: BOOST_STATIC_ASSERT(false) In templated code that should be unreachable. However, its doesn't work with compilers that are too smart for their own good. In order to make this work I need something like: BOOST_STATIC_ASSERT(boost::false_<T>::value) Where boost::false<T> is a type that yields false for any type T. Am I correct here? Where should such a code snippet be placed? Could it be added to mpl, type_trait or what? Robert Ramey

Robert Ramey wrote:
BOOST_STATIC_ASSERT(false)
This *should* work. IMHO, if it doesn't, we should fix it, not work around it like Michiel suggested.
However, its doesn't work with compilers that are too smart for their own good. In order to make this work I need something like:
Which compiler and which version of boost and the Compiler are you using? Can you provide a minimal but complete example? Also, you might want to have a look at checked_delete where we had similar problems in the past IIRC. Regards, Daniel -- Daniel Frey aixigo AG - financial solutions & technology Schloß-Rahe-Straße 15, 52072 Aachen, Germany fon: +49 (0)241 936737-42, fax: +49 (0)241 936737-99 eMail: daniel.frey@aixigo.de, web: http://www.aixigo.de

On Thu, Jun 24, 2004 at 05:51:15PM +0200, Daniel Frey wrote:
Robert Ramey wrote:
BOOST_STATIC_ASSERT(false)
This *should* work. IMHO, if it doesn't, we should fix it, not work around it like Michiel suggested.
BOOST_STATIC_ASSERT(false) expands to a typedef that does not contain any dependent expression. I didn't check the detailed rules in my copy of the template book by Vandevoorde and Josuttis, but I think a compiler should check this typedef even if the function template is not instantiated.
However, its doesn't work with compilers that are too smart for their own good. In order to make this work I need something like:
Which compiler and which version of boost and the Compiler are you using? Can you provide a minimal but complete example? Also, you might want to have a look at checked_delete where we had similar problems in the past IIRC.
Try the following program: #include <boost/static_assert.hpp> template <typename T> void f(T t) { #ifdef DEPENDENT_ASSERT_CONDITION BOOST_STATIC_ASSERT(sizeof(T) && false); #else BOOST_STATIC_ASSERT(false); #endif } #ifdef INSTANTIATE_F void g(int i) { f(i); } #endif g++ 3.3.1 accepts it unless you define INSTANTIATE_F. g++ 3.4.0 rejects it unless you define DEPENDENT_ASSERT_CONDITION (and don't define INSTANTIATE_F, of course). I don't know if my solution (sizeof(T) && false) has any (dis-)advantages compared to Michiel's solution ((T*) 0). Regards Christoph PS: Ramey, I patched your serialization library (based on serialization18.zip) such that g++ 3.4 compiles the examples. Besides above problem and the many errors due to the unqualified use of dependent names I had to work around a bug in libstdc++v3 (PR#16154). But I didn't have the time yet for extensive tests. If you are interested then I can send you my patches. -- http://www.informatik.tu-darmstadt.de/TI/Mitarbeiter/cludwig.html LiDIA: http://www.informatik.tu-darmstadt.de/TI/LiDIA/Welcome.html

Christoph Ludwig wrote:
On Thu, Jun 24, 2004 at 05:51:15PM +0200, Daniel Frey wrote:
Robert Ramey wrote:
BOOST_STATIC_ASSERT(false)
This *should* work. IMHO, if it doesn't, we should fix it, not work around it like Michiel suggested.
BOOST_STATIC_ASSERT(false) expands to a typedef that does not contain any dependent expression. I didn't check the detailed rules in my copy of the template book by Vandevoorde and Josuttis, but I think a compiler should check this typedef even if the function template is not instantiated.
The compiler is allowed but not required to reject a template that can never produce a legal instantiation. [...]
I don't know if my solution (sizeof(T) && false) has any (dis-)advantages compared to Michiel's solution ((T*) 0).
A compiler is allowed to reject both. I think that you need one additional level of indirection: template<class T> struct always_false { enum { value = 0 }; }; template<class T> struct always_assert { BOOST_STATIC_ASSERT( always_false<T>::value ); };
participants (4)
-
Christoph Ludwig
-
Daniel Frey
-
Peter Dimov
-
Robert Ramey