Static Assert
-
Da ich keinen Gebrauch von C++11 machen kann muss ich mir ein Workaround für ein static_assert bauen. Ich möchte ungern Templates mit Preprozessor Direktiven mischen, weiss aber keine bessere Lösung als das Macro SOMETHING_IS_OK() enzusetzen. Gibts hier eine alternative Implementierung ohne Verwendung eines Macros?
#include <assert.h> // assert // Alternative implementation for static_assert() C++11. template<bool T> class StaticAssert; template<> class StaticAssert<true> {}; #define STATIC_ASSERT(expression) StaticAssert<(expression)>() // Returns false to trigger a compile time assertion. #define SOMETHING_IS_OK(x) ((x) == 123456 || (x) == 234567) // Returns false to trigger a runtime assertion. inline bool SomethingIsOK(int x) { return x == 123456 || x == 234567; } template<int COMPILE_TIME> void DoNothing(int runtime) { STATIC_ASSERT(SOMETHING_IS_OK(COMPILE_TIME)); assert(SomethingIsOK(runtime)); }
-
Dann lass das Makro doch weg und benutz das Template direkt.