BOOST_STATIC_ASSERT and g++ 3.4

The code below works as expected when compiled with g++ 3.4 as: $ g++ -I$HOME/boost/cvs -g -W -Wall -o static_test2 static_test2.cpp but it also produces this warning: static_test2.cpp: In static member function `static foo foo::set()': static_test2.cpp:15: warning: no return statement in function returning non-void Clearly, I could add a spurious return statement after the assert, but isn't g++ being a little over eager here? Can the warning not be silenced by the library? Regards, Angus #include <boost/static_assert.hpp> #include <iostream> class foo { public: enum state { state1, state2, state3 }; // Only the specializations (below) of this template will compile. template <int N> static foo set() { BOOST_STATIC_ASSERT( N == state2 ); } private: foo(state) { std::cout << "foo" << std::endl; } }; template <> foo foo::set<foo::state2>() { return foo::state2; } template <> foo foo::set<foo::state3>() { return foo::state3; } int main() { // Compiles, as expected. foo f1 = foo::set<foo::state2>(); foo f2(foo::set<foo::state2>()); // Fail to compile, as expected. // foo f3 = foo::set<foo::state1>(); // foo f4(foo::set<foo::state1>()); return 0; }

The code below works as expected when compiled with g++ 3.4 as:
$ g++ -I$HOME/boost/cvs -g -W -Wall -o static_test2 static_test2.cpp
but it also produces this warning:
static_test2.cpp: In static member function `static foo foo::set()': static_test2.cpp:15: warning: no return statement in function returning non-void
Clearly, I could add a spurious return statement after the assert, but isn't g++ being a little over eager here? Can the warning not be silenced by the library?
None: it would have to introduce a return statement to do so, and that would break so many things that I don't know where to begin. Remember that many uses of BOOST_STATIC_ASSERT do expect it to compile, and generate neither code not data. John.

John Maddock wrote:
Clearly, I could add a spurious return statement after the assert, but isn't g++ being a little over eager here? Can the warning not be silenced by the library?
None: it would have to introduce a return statement to do so, and that would break so many things that I don't know where to begin. Remember that many uses of BOOST_STATIC_ASSERT do expect it to compile, and generate neither code not data.
So the __noreturn__ command used by GNU's /usr/include/assert.h couldn't be used? /* This prints an "Assertion failed" message and aborts. */ extern void __assert_fail (__const char *__assertion, __const char *__file, unsigned int __line, __const char *__function) __THROW __attribute__ ((__noreturn__)); Oh well. Thanks. Angus

So the __noreturn__ command used by GNU's /usr/include/assert.h couldn't be used?
/* This prints an "Assertion failed" message and aborts. */ extern void __assert_fail (__const char *__assertion, __const char *__file, unsigned int __line, __const char *__function) __THROW __attribute__ ((__noreturn__));
Wouldn't that have to be applied to your function signature, rather than the assertion? John.

John Maddock wrote:
So the __noreturn__ command used by GNU's /usr/include/assert.h couldn't be used?
/* This prints an "Assertion failed" message and aborts. */ extern void __assert_fail (__const char *__assertion, __const char *__file, unsigned int __line, __const char *__function) __THROW __attribute__ ((__noreturn__));
Wouldn't that have to be applied to your function signature, rather than the assertion?
Well, code like this doesn't trigger a warning when compiled with g++ $ cat trial.cpp #include <cassert> int foo(bool a) { if (a) return 0; assert(false); } $ g++ -W -Wall -c trial.cpp Compare that with: $ g++ -DNDEBUG -W -Wall -c trial.cpp trial.cpp: In function `int foo(bool)': trial.cpp:7: warning: control reaches end of non-void function Clearly, the compiler is able to ascertain that execution won't proceed past the assert. It doesn't seem so unreasonable to hope that BOOST_STATIC_ASSERT would behave similarly. Regards, Angus

Well, code like this doesn't trigger a warning when compiled with g++
$ cat trial.cpp #include <cassert>
int foo(bool a) { if (a) return 0; assert(false); }
$ g++ -W -Wall -c trial.cpp
Compare that with:
$ g++ -DNDEBUG -W -Wall -c trial.cpp trial.cpp: In function `int foo(bool)': trial.cpp:7: warning: control reaches end of non-void function
Clearly, the compiler is able to ascertain that execution won't proceed past the assert. It doesn't seem so unreasonable to hope that BOOST_STATIC_ASSERT would behave similarly.
The problem is that __attribute__((noreturn)) can only be applied to a *function call which will never return*, a static assertion does not generate a function call (indeed must not do so), so there is nothing to which that attribute can be applied. If you know differently then I'm all ears, Regards, John.
participants (2)
-
Angus Leeming
-
John Maddock