Static statements are very convenient for checking things at compile time. A simple static idiom of a statement looks like this:
template<bool> struct StaticAssert;
template<> struct StaticAssert<true> {};
#define STATIC_ASSERT(condition) do { StaticAssert<(condition)>(); } while(0)
This is useful for things like
STATIC_ASSERT(sizeof(float) == 4)
and
#define THIS_LIMIT (1000)
...
STATIC_ASSERT(THIS_LIMIT > OTHER_LIMIT);
But use is #definenot a "C ++" way of defining constants. C ++ will use an anonymous namespace:
namespace {
const int THIS_LIMIT = 1000;
}
or even:
static const int THIS_LIMIT = 1000;
The problem is that with const intyou cannot use STATIC_ASSERT(), and you must resort to checking the runtime, which is stupid.
Is there a way to solve this correctly in current C ++?
I think I read C ++ 0x has some features for this ...
EDIT
So this is
static const int THIS_LIMIT = 1000;
...
STATIC_ASSERT(THIS_LIMIT > 0);
compiles fine
But this:
static const float THIS_LIMIT = 1000.0f;
...
STATIC_ASSERT(THIS_LIMIT > 0.0f);
no.
(in Visual Studio 2008)
How did it happen?