Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template<typename T, typename U>
- inline constexpr T type_pun(const U & x) {
- static_assert(std::is_trivial<T>::value && std::is_trivial<U>::value, "Cannot use type_pun with non-trivial data" );
- static_assert(sizeof(T) == sizeof(U), "type_pun must be used on identically sized types");
- union in_out {
- U in;
- T out;
- inline constexpr explicit in_out(const U &x) : in(x)
- {
- }
- inline T get() const {
- return const_cast<const T&>(out);
- }
- };
- return in_out(in_out(x)).get();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement