mgcpp
A C++ Math Library Based on CUDA
pun_cast.hpp
Go to the documentation of this file.
1 
2 // Copyright RedPortal, mujjingun 2017 - 2018.
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 
7 #ifndef _MGCPP_PUN_CAST_HPP_
8 #define _MGCPP_PUN_CAST_HPP_
9 
10 #include <type_traits>
11 
12 namespace mgcpp {
13 template <typename To, typename From>
14 inline To pun_cast(From const* from) {
15  union pun_t {
16  From src;
17  typename std::remove_pointer<To>::type dst;
18  };
19  return &(reinterpret_cast<pun_t const*>(from))->dst;
20 }
21 
22 template <typename To, typename From>
23 inline To pun_cast(From* from) {
24  union pun_t {
25  From src;
26  typename std::remove_pointer<To>::type dst;
27  };
28  return &(reinterpret_cast<pun_t*>(from))->dst;
29 }
30 } // namespace mgcpp
31 
32 #endif
Definition: adapter_base.hpp:12
To pun_cast(From const *from)
Definition: pun_cast.hpp:14