Last active
November 16, 2016 23:21
-
-
Save jessestricker/528a32edb39fe33e42432600287f2aa4 to your computer and use it in GitHub Desktop.
Util Functions for Type Casting: pointer-to-pointer & enum-integral
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <type_traits> | |
// ======== enum_cast | |
template <class E> | |
constexpr std::enable_if_t<std::is_enum<E>::value, std::underlying_type_t<E>> enum_cast(E e) noexcept | |
{ | |
return static_cast<std::underlying_type_t<E>>(e); | |
} | |
// ======== pointer_cast | |
template <class TT, class FT> | |
constexpr auto pointer_cast(FT* p) noexcept | |
{ | |
return static_cast<TT*>(static_cast<void*>(p)); | |
} | |
template <class TT, class FT> | |
constexpr auto pointer_cast(FT const* p) noexcept | |
{ | |
return static_cast<TT const*>(static_cast<void const*>(p)); | |
} | |
template <class TT, class FT> | |
constexpr auto pointer_cast(FT volatile* p) noexcept | |
{ | |
return static_cast<TT volatile*>(static_cast<void volatile*>(p)); | |
} | |
template <class TT, class FT> | |
constexpr auto pointer_cast(FT const volatile* p) noexcept | |
{ | |
return static_cast<TT const volatile*>(static_cast<void const volatile*>(p)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment