Typeid.hpp
Go to the documentation of this file.
1 //
3 // Aurora C++ Library
4 // Copyright (c) 2012-2016 Jan Haller
5 //
6 // This software is provided 'as-is', without any express or implied
7 // warranty. In no event will the authors be held liable for any damages
8 // arising from the use of this software.
9 //
10 // Permission is granted to anyone to use this software for any purpose,
11 // including commercial applications, and to alter it and redistribute it
12 // freely, subject to the following restrictions:
13 //
14 // 1. The origin of this software must not be misrepresented; you must not
15 // claim that you wrote the original software. If you use this software
16 // in a product, an acknowledgment in the product documentation would be
17 // appreciated but is not required.
18 //
19 // 2. Altered source versions must be plainly marked as such, and must not be
20 // misrepresented as being the original software.
21 //
22 // 3. This notice may not be removed or altered from any source distribution.
23 //
25 
28 
29 #ifndef AURORA_TYPEID_HPP
30 #define AURORA_TYPEID_HPP
31 
33 
34 #include <typeindex>
35 #include <type_traits>
36 
37 
38 namespace aurora
39 {
40 namespace detail
41 {
42 
43  template <typename T>
44  void verifyRttiType()
45  {
47  static_assert(std::is_polymorphic<T>::value, "T must be a polymorphic type.");
48 #ifndef AURORA_TYPEID_ALLOW_NONVIRTUAL_DTOR
49  static_assert(std::has_virtual_destructor<T>::value, "Polymorphic types should have a virtual destructor. "
50  "Define AURORA_TYPEID_ALLOW_NONVIRTUAL_DTOR to let this code compile.");
51 #endif
52  }
53 
54 } // namespace detail
55 
58 
75 template <typename T>
76 std::type_index typeIndex(T& reference)
77 {
78  detail::verifyRttiType<T>();
79  return typeid(reference);
80 }
81 
89 template <typename T>
90 std::type_index typeIndex()
91 {
92  detail::verifyRttiType<T>();
93  return typeid(T);
94 }
95 
96 
98 
99 } // namespace aurora
100 
101 #endif // AURORA_TYPEID_HPP
Utilities for template metaprogramming.
#define AURORA_REQUIRE_COMPLETE_TYPE(Type)
Macro to ensure complete type.
Definition: Templates.hpp:190
std::type_index typeIndex()
Safe typeid operator for types.
Definition: Typeid.hpp:90
Definition: DispatchTraits.hpp:39