Hi,
I am trying to compile the following program, whose fragments are
presented in Section 2.1: "Compile-Time Assertions" of Chapter 2:
"Techniques" of "Modern C++ Design" by Andrei Alexandrescu, but
couldn't;
////////////////////////////////////////////////////////////////////////////////////////////////////////
template<boolstruct CompileTimeChecker
{
CompileTimeChecker(...);
};
template<struct CompileTimeChecker<false{ };
#define STATIC_CHECK(expr, msg) \
{\
class ERROR_##msg {}; \
(void)sizeof(CompileTimeChecker<(expr) != 0>((ERROR_##msg())));\
}
template <class To, class From>
To safe_reinterpret_cast(From from)
{
STATIC_CHECK(sizeof(From) <= sizeof(To),
Destination_Type_Too_Narrow);
return reinterpret_cast<To>(from);
}
int main(int argc, char* argv[]){
void* aptr = 0;
char c = safe_reinterpret_cast<char>(aptr);
return 0;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
As per the book, an expected result should be a compile-time error
occurence due to the "STATIC_CHECK" macro. But this hasn't been the
case with a couple of compilers (gcc, Comeau), I tried to compile this
program. I find both compiler's normal "reinterpret_cast" performing
the functionality of "safe_reinterpret_cast". Apart from these, I am
also getting an error "invalid application of `sizeof' to a function
type" for the statement "(void)sizeof(CompileTimeChecker<(expr) !=
0>((ERROR_##msg())));".
Can somebody please clarify whether anything is amiss with the above
program?
Thanks in advance!