openMSX
checked_cast.hh
Go to the documentation of this file.
1 #ifndef CHECKED_CAST_HH
2 #define CHECKED_CAST_HH
3 
10 #include <type_traits>
11 #include <cassert>
12 
13 template<typename TO, typename FROM>
14 static TO checked_cast(FROM* from)
15 {
16  assert(dynamic_cast<TO>(from) == static_cast<TO>(from));
17  return static_cast<TO>(from);
18 }
19 template<typename TO, typename FROM>
20 static TO checked_cast(FROM& from)
21 {
22  typedef typename std::remove_reference<TO>::type* TO_PTR;
23  TO_PTR* suppress_warning = nullptr; (void)suppress_warning;
24  assert(dynamic_cast<TO_PTR>(&from) == static_cast<TO_PTR>(&from));
25  return static_cast<TO>(from);
26 }
27 
28 #endif