Line data Source code
1 0 : // Distributed under the MIT License.
2 : // See LICENSE.txt for details.
3 :
4 : #pragma once
5 :
6 : #include <type_traits>
7 : #include <utility>
8 :
9 : #include "Utilities/Requires.hpp"
10 :
11 : namespace tt {
12 : namespace detail {
13 : template <typename T, typename U, typename = void>
14 : struct can_static_cast : std::false_type {};
15 :
16 : template <typename T, typename U>
17 : struct can_static_cast<T, U,
18 : std::void_t<decltype(static_cast<U>(std::declval<T>()))>>
19 : : std::true_type {};
20 :
21 : template <typename T, typename U>
22 : static constexpr bool can_static_cast_v = can_static_cast<T, U>::value;
23 : } // namespace detail
24 :
25 : /*!
26 : * \brief Cast `t` which is of type `T*` to a `U`. If a `static_cast<U>(t)` is
27 : * possible, use that, otherwise use `dynamic_cast<U>(t)`.
28 : */
29 : template <typename U, typename T>
30 1 : U fast_pointer_cast(T* t) {
31 : if constexpr (detail::can_static_cast_v<T*, U>) {
32 : return static_cast<U>(t);
33 : } else {
34 : return dynamic_cast<U>(t);
35 : }
36 : }
37 : } // namespace tt
|