AFF --- A container for numbers (array) by Friederich and Forbriger.
checkedcast.h
Go to the documentation of this file.
1 
37 // include guard
38 #ifndef AFF_CHECKEDCAST_H_VERSION
39 
40 #define AFF_CHECKEDCAST_H_VERSION \
41  "AFF_CHECKEDCAST_H V1.0"
42 
43 namespace aff {
44 namespace util {
45 
83  template<class T, class TT>
85 
86  public:
88  static TT* cast(T* p) {
89  return(reinterpret_cast<TT*>(
90  SizeCheck<sizeof(T),sizeof(TT)>::have_same_size(
92 
93  private:
110  template<class Tfrom, class Tto> class ConstCheck {
111  public: static T* respects_constness(T* v) { return v; }
112  }; // class ConstCheck
113 
123  template<class Tfrom, class Tto>
124  class ConstCheck<Tfrom, const Tto> {
125  public: static T* respects_constness(T* v) { return v; }
126  }; // class ConstCheck (specialization)
127 
137  template<class Tfrom, class Tto>
138  class ConstCheck<const Tfrom, const Tto> {
139  public: static T* respects_constness(T* v) { return v; }
140  }; // class ConstCheck (specialization)
141 
151  template<class Tfrom, class Tto>
152  class ConstCheck<const Tfrom, Tto>
153  { }; // class ConstCheck (specialization)
154 
155  /*----------------------------------------------------------------*/
156 
170  template<int N, int M> class SizeCheck
171  { }; // class SizeCheck (specialization)
172 
181  template<int N> class SizeCheck<N,N> {
182  public: static T* have_same_size(T* v) { return v; }
183  }; // class SizeCheck
184 
185  }; // class SizeCheckedCast
186 
187 } // namespace util
188 } // namespace aff
189 
190 #endif // AFF_CHECKEDCAST_H_VERSION (includeguard)
191 
192 /* ----- END OF checkedcast.h ----- */
Root namespace of library.
Definition: array.h:148
utility for compile-time checked cast
Definition: checkedcast.h:84
checks for matching type-size
Definition: checkedcast.h:170
static TT * cast(T *p)
return a type-casted (and type-checked) pointer
Definition: checkedcast.h:88
checks for const-correctness of the cast
Definition: checkedcast.h:110