On Apr 9, 2:08 pm, "nevergone" <wyx2006s...@gmail.com> wrote:
so my question is why " (void)sizeof( CompileTimeChecker< (expr) != 0>( (ERROR_##msg() ) )); \" must add sizeof?
in general, checks for incomplete types are better done within sizeof
because it's runtime overheads are minimum. try to generate the
assembly code (using, g++ -S) with and without the "sizeof" and you'll
see that the sizeof version is better.
however, i have a related, but different problem. i tried the code as
printed in the book[#1] and couldn't get it to work.
---
/* begin 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()))); }
int main(){ STATIC_CHECK(1>2,mustFail);}
/* end code */
---
here's what g++ gives me:
---> g++ -Wall -pedantic test.cpp
test.cpp: In function `int main()':
test.cpp:4: warning: ISO C++ forbids applying `sizeof' to a function
type
---
apart from that warning, the compilation goes through.
shouldn't it fail? what am i missing?
thanks,
--
#1 - pg. 21, Modern C++ Design, Andrei Alexandrescu.
I don't know.