Consider the following code:
template<bool> class StaticAssert;
template<> class StaticAssert<true> {};
StaticAssert< (-1 < sizeof(int)) > xyz1; // Compile error
StaticAssert< (-1 > sizeof(int)) > xyz2; // OK
Why is -1 > sizeof(int) true?
- Is it true that
-1is promoted tounsigned(-1)and thenunsigned(-1) > sizeof(int). - Is it true that
-1 > sizeof(int)is equivalent to-1 > size_t(4)if sizeof(int) is 4. If this is so why-1 > size_t(4)is false?
Is this C++ standard comformant?
The following is how standard (ISO 14882) explains abort -1 > sizeof(int)
Relational operator `>’ is defined in 5.9 (expr.rel/2)
The usual arithmetic conversions is defined in 5 (expr/9)
… The pattern is called the usual arithmetic conversions, which are defined as following:
double, …
The integral promotions is defined in 4.5 (conv.prom/1)
The result of sizeof is defined in 5.3.3 (expr.sizeof/6)
size_t is defined in C standard (ISO 9899), which is unsigned integer type.
So for
-1 > sizeof(int), the > triggers usual arithmetic conversions. The usual arithmetic conversion converts -1 to unsigned int because int cannot represent all the value ofsize_t.-1becomes a very large number depend on platform. So-1 > sizeof(int)istrue.