Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CompileTimeChecker from Modern C++ Design not working as expected

I've recently started reading Modern C++ Design by Andrei Alexandrescu. After reading Compile-Time Assertions, 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 change the Line 1 to

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

or

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

it works as expected. I don't get it.

like image 536
Saurabh Manchanda Avatar asked Nov 06 '22 08:11

Saurabh Manchanda


1 Answers

Try updated version from the Loki library.

like image 175
Nikolai Fetissov Avatar answered Nov 09 '22 06:11

Nikolai Fetissov