IsMaplike.hpp
1 // Distributed under the MIT License.
2 // See LICENSE.txt for details.
3 
4 #pragma once
5 
6 #include <type_traits>
7 
8 #include "Utilities/Requires.hpp"
9 #include "Utilities/TypeTraits.hpp"
10 #include "Utilities/TypeTraits/IsIterable.hpp"
11 
12 namespace tt {
13 // @{
14 /// \ingroup TypeTraitsGroup
15 /// \brief Check if type `T` is like a std::map or std::unordored_map
16 ///
17 /// \details
18 /// Inherits from std::true_type if the type `T` has a type alias `key_type`,
19 /// type alias `mapped_type`, and `operator[](const typename T::key_type&)`
20 /// defined, otherwise inherits from std::false_type
21 ///
22 /// \usage
23 /// For any type `T`,
24 /// \code
25 /// using result = tt::is_maplike<T>;
26 /// \endcode
27 ///
28 /// \metareturns
29 /// cpp17::bool_constant
30 ///
31 /// \semantics
32 /// If the type `T` has a type alias `key_type`,
33 /// type alias `mapped_type`, and `operator[](const typename T::key_type&)`
34 /// defined, then
35 /// \code
36 /// typename result::type = std::true_type;
37 /// \endcode
38 /// otherwise
39 /// \code
40 /// typename result::type = std::false_type;
41 /// \endcode
42 ///
43 /// \example
44 /// \snippet Test_IsMaplike.cpp is_maplike_example
45 /// \see std::map std::unordered_map is_a
46 /// \tparam T the type to check
47 template <typename T, typename = cpp17::void_t<>>
49 
50 /// \cond
51 template <typename T>
52 struct is_maplike<T,
53  cpp17::void_t<typename T::key_type, typename T::mapped_type,
54  decltype(std::declval<T&>()[std::declval<
55  const typename T::key_type&>()]),
56  Requires<tt::is_iterable_v<T>>>>
57  : std::true_type {};
58 /// \endcond
59 
60 /// \see is_maplike
61 template <typename T>
63 
64 /// \see is_maplike
65 template <typename T>
67 // @}
68 } // namespace tt
void void_t
Given a set of types, returns void
Definition: TypeTraits.hpp:187
Defines the type alias Requires.
A collection of useful type traits.
Definition: TensorExpression.hpp:113
typename is_maplike< T >::type is_maplike_t
Definition: IsMaplike.hpp:66
constexpr bool is_maplike_v
Definition: IsMaplike.hpp:62
Check if type T is like a std::map or std::unordored_map.
Definition: IsMaplike.hpp:48