File indexing completed on 2025-01-18 09:38:12
0001
0002
0003
0004
0005
0006
0007 #ifndef BOOST_HISTOGRAM_DETAIL_TRY_CAST_HPP
0008 #define BOOST_HISTOGRAM_DETAIL_TRY_CAST_HPP
0009
0010 #include <boost/config.hpp> // BOOST_NORETURN
0011 #include <boost/throw_exception.hpp>
0012 #include <type_traits>
0013
0014 namespace boost {
0015 namespace histogram {
0016 namespace detail {
0017
0018 template <class T, class U>
0019 constexpr T* ptr_cast(U*) noexcept {
0020 return nullptr;
0021 }
0022
0023 template <class T>
0024 constexpr T* ptr_cast(T* p) noexcept {
0025 return p;
0026 }
0027
0028 template <class T>
0029 constexpr const T* ptr_cast(const T* p) noexcept {
0030 return p;
0031 }
0032
0033 template <class T, class E, class U>
0034 BOOST_NORETURN T try_cast_impl(std::false_type, std::false_type, U&&) {
0035 BOOST_THROW_EXCEPTION(E("type cast error"));
0036 }
0037
0038
0039 template <class T, class E, class U>
0040 T try_cast_impl(std::false_type, std::true_type, U&& u) noexcept {
0041 return static_cast<T>(u);
0042 }
0043
0044
0045 template <class T, class E>
0046 T&& try_cast_impl(std::true_type, std::true_type, T&& t) noexcept {
0047 return std::forward<T>(t);
0048 }
0049
0050
0051 template <class T, class E, class U>
0052 T try_cast(U&& u) noexcept(std::is_convertible<U, T>::value) {
0053 return try_cast_impl<T, E>(std::is_same<U, T>{}, std::is_convertible<U, T>{},
0054 std::forward<U>(u));
0055 }
0056
0057 }
0058 }
0059 }
0060
0061 #endif