Compile-Time Assertions
From Modern C++ Design, S. Alexandrescu
It's a technique that relies in the fact that a zero-length array is ilegal:
#define STATIC_CHECK(expr) { char unnamed[(expr) ? 1 : 0]; }
To safe_reinterpret_cast(From from)
{
STATIC_CHECK(sizeof(From) <= sizeof(To));
return reinterpret_cast<To>(from);
}
...
void* somePointer = ...;
char c = safe_reinterpret_cast<char>(somePointer);
template <class To, class From>Cons: The error message you receive is not terribly informative "Cannot create array of size zero"
Comentarios
Publicar un comentario