Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.56 KB | None | 0 0
  1. template<typename T, typename U>
  2. inline constexpr T type_pun(const U & x) {
  3.     static_assert(std::is_trivial<T>::value && std::is_trivial<U>::value, "Cannot use type_pun with non-trivial data" );
  4.     static_assert(sizeof(T) == sizeof(U), "type_pun must be used on identically sized types");
  5.     union in_out {
  6.         U in;
  7.         T out;
  8.  
  9.         inline constexpr explicit in_out(const U &x) : in(x)
  10.         {
  11.         }
  12.  
  13.         inline T get() const {
  14.             return const_cast<const T&>(out);
  15.         }
  16.     };
  17.     return in_out(in_out(x)).get();
  18. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement