Horizon
polymorphic_cast.hpp
1 // (C) Copyright Kevlin Henney and Dave Abrahams 1999.
2 // Distributed under the Boost
3 // Software License, Version 1.0. (See accompanying file
4 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5 
6 #ifndef RANGES_V3_UTILITY_POLYMORPHIC_CAST_HPP
7 #define RANGES_V3_UTILITY_POLYMORPHIC_CAST_HPP
8 
9 #include <memory>
10 #include <type_traits>
11 
12 #include <meta/meta.hpp>
13 
14 #include <range/v3/detail/config.hpp>
15 
16 #include <range/v3/detail/prologue.hpp>
17 
18 namespace ranges
19 {
20  template<typename Target, typename Source>
21  auto polymorphic_downcast(Source * x) noexcept
23  decltype((static_cast<Target>(x), dynamic_cast<Target>(x)))>
24  {
25  auto result = static_cast<Target>(x);
26  RANGES_ASSERT(dynamic_cast<Target>(x) == result);
27  return result;
28  }
29  template<typename Target, typename Source>
30  auto polymorphic_downcast(Source && x) noexcept
32  decltype((static_cast<Target>(std::declval<Source>()),
33  dynamic_cast<Target>(std::declval<Source>())))>
34  {
35  auto && result = static_cast<Target>(static_cast<Source &&>(x));
36 #ifndef NDEBUG
37  auto && dresult = dynamic_cast<Target>(static_cast<Source &&>(x));
38  RANGES_ASSERT(std::addressof(dresult) == std::addressof(result));
39 #endif
40  return static_cast<Target>(result);
41  }
42 } // namespace ranges
43 
44 #include <range/v3/detail/epilogue.hpp>
45 
46 #endif // RANGES_V3_UTILITY_POLYMORPHIC_CAST_HPP
_t< detail::_if_< list< Args... > >> if_
Select one type or another depending on a compile-time Boolean.
Definition: meta.hpp:1247
Tiny meta-programming library.