In C++11, should the operation of static_assert within a template depend on whether that template has been instantiated or not? For example, with the following code
template <int I>
void sa() { static_assert(0,"Hello."); }
int main(int argc, char *argv[]) { return 0; }
GCC 4.5.0 will fail the assertion, and produce the “Hello.” message.
The Digital Mars Compiler version 8.42n on the other hand, gives no message.
GCC is correct and the other compiler is correct too. Refer to 14.6p8 in the spec
Therefor, a compiler is free to reject the following
If you want to go safe, you have to arrange it so the compiler cannot know until instantiation whether the boolean expression will be true or false. For example, get the value by
getvalue<T>::value, withgetvaluebeing a class template (one could specialize it, so the compiler cannot possibly know the boolean value already).