CompileTimeChecker from Modern C ++ Design does not work as expected

I recently started reading Modern C ++ Design by Andrei Alexandrescu. After reading compile-time, I tried the following code:

template <bool> struct CompileTimeChecker
{
    CompileTimeChecker (...) {};
};
template <> struct CompileTimeChecker <false> {};

#define STATIC_CHECK (expr, msg) \
{\
    class ERROR _ ## msg {}; \
    (void) sizeof (CompileTimeChecker <(expr)! = 0> ((ERROR _ ## msg ()))); / * Line 1 * /}


int main ()
{
    STATIC_CHECK (sizeof (char)> sizeof (int), TypeTooNarrow); / * Line 2 * /

    STATIC_CHECK (sizeof (char) <sizeof (int), TypeTooNarrow); / * Line 3 * /
}

The code should not compile due to line 2, but it compiles fine. If I changed line 1 to

(void)(CompileTimeChecker<(expr)!=0>((ERROR_##msg())));   /*Line 1*/ }

new CompileTimeChecker<(expr)!=0>((ERROR_##msg()));   /* Line 1*/ }

, . .

+5
1
+2

All Articles