1 // Copyright 2019 Hans Dembinski
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt
5 // or copy at http://www.boost.org/LICENSE_1_0.txt)
7 #ifndef BOOST_HISTOGRAM_DETAIL_TRY_CAST_HPP
8 #define BOOST_HISTOGRAM_DETAIL_TRY_CAST_HPP
10 #include <boost/config.hpp> // BOOST_NORETURN
11 #include <boost/throw_exception.hpp>
12 #include <type_traits>
18 template <class T, class U>
19 constexpr T* ptr_cast(U*) noexcept {
24 constexpr T* ptr_cast(T* p) noexcept {
29 constexpr const T* ptr_cast(const T* p) noexcept {
33 template <class T, class E, class U>
34 BOOST_NORETURN T try_cast_impl(std::false_type, std::false_type, U&&) {
35 BOOST_THROW_EXCEPTION(E("type cast error"));
39 template <class T, class E, class U>
40 T try_cast_impl(std::false_type, std::true_type, U&& u) noexcept {
41 return static_cast<T>(u); // cast to avoid warnings
45 template <class T, class E>
46 T&& try_cast_impl(std::true_type, std::true_type, T&& t) noexcept {
47 return std::forward<T>(t);
50 // cast fails at runtime with exception E instead of compile-time, T must be a value
51 template <class T, class E, class U>
52 T try_cast(U&& u) noexcept(std::is_convertible<U, T>::value) {
53 return try_cast_impl<T, E>(std::is_same<U, T>{}, std::is_convertible<U, T>{},
58 } // namespace histogram