AFF --- A container for numbers (array) by Friederich and Forbriger.
|
utility for compile-time checked cast More...
#include <checkedcast.h>
Classes | |
class | ConstCheck |
checks for const-correctness of the cast More... | |
class | ConstCheck< const Tfrom, const Tto > |
specialization for const-checked cast More... | |
class | ConstCheck< const Tfrom, Tto > |
specialization for const-checked cast More... | |
class | ConstCheck< Tfrom, const Tto > |
specialization for const-checked cast More... | |
class | SizeCheck |
checks for matching type-size More... | |
class | SizeCheck< N, N > |
specialization for type-size checked cast More... | |
Static Public Member Functions | |
static TT * | cast (T *p) |
return a type-casted (and type-checked) pointer More... | |
utility for compile-time checked cast
Cast a pointer from T* to TT* and check for same size of values of type T and TT and for const-correctness of the conversion. Although reinterpret_cast should check for conts-correctness, we implement a check here too (just for safety - I'm so paranoid, you know).
When exchaning data with Fortran code we have to pass pointers to arrays. The f2c.h uses a structure (e.g.) for values of type doublereal that have the same memory layout like complex<double> (first a double for the real part than a double for the imaginary part) but there is no trivial conversion between pointers of these types. Hence we have to use reinterpret_cast, which is totally unchecked. With this class we implement a cast that checks for matching type-size and const-correctness at compile-time. The involved code-overhead is easily optimized away by the compiler.
There could be solutions to this problem that involve less code. This however is a solution that produces well understandable error messages with g++.
Invoking static functions of subclasses in the cast-function may seem unnecessary. Indeed, there is a solution of that problem, that relies on typedefs to be resolved, which works well with g++. Since I don't know about the template instatiation characteristics of other compilers, I prefer to force the compiler to instantiate all involved template specializations to check whether they support the cast.
T | the input pointer is of type T* |
TT | the output pointer is of type TT* |
Definition at line 84 of file checkedcast.h.