AFF --- A container for numbers (array) by Friederich and Forbriger.
aff::util::SizeCheckedCast< T, TT > Class Template Reference

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...
 

Detailed Description

template<class T, class TT>
class aff::util::SizeCheckedCast< T, TT >

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).

See also
aff::util::SizeCheckedCast::ConstCheck
aff::util::SizeCheckedCast::SizeCheck

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.

See also
aff::FortranArray
tests/f77test.cc
Parameters
Tthe input pointer is of type T*
TTthe output pointer is of type TT*

Definition at line 84 of file checkedcast.h.


The documentation for this class was generated from the following file: