Template Class Key¶
Defined in File Key.h
Class Documentation¶
-
template<typename
K
, typenameV
>
classKey
Key for type-safe lookup in a GenericMap.
Key objects are equality-comparable, hashable, sortable, or printable if and only if
K
is comparable, hashable, sortable, or printable, respectively. Key can be used in compile-time expressions if and only ifK
can (in particular,Key<std::string, V>
cannot).- Template Parameters
K
: the logical type of the key (e.g., a string)V
: the type of the value mapped to this key
Unnamed Group
-
constexpr bool
operator==
(Key<K, V> const &other) const¶ Test for key equality.
A key is considered equal to another key if and only if their getId() are equal and their value types are exactly the same (including const/volatile qualifications).
Public Functions
-
constexpr
Key
(K id)¶ Construct a new key.
Provides the same exception safety as the copy-constructor of
K
.- Parameters
id
: the identifier of the field. For most purposes, this value is the actual key; it can be retrieved by calling getId().
- See
-
Key &
operator=
(Key const&)¶
-
Key &
operator=
(Key&&)¶
-
template<typename
U
>Key
(Key<K, U> other)¶ Convert a key to a different key that could retrieve the same values.
- Note
This constructor is intended to support implicit conversions.
- Template Parameters
U
: The value type being converted from. Smart pointer keys are convertible if and only if the corresponding pointers are; other keys are convertible if and only if their value references are (consistent with pointers being retrieved by value and other types by reference).
- Parameters
other
:
-
constexpr K const &
getId
() const¶ Return the identifier of this field.
The identifier serves as the “key” for the map abstraction represented by GenericMap.
- Return
the unique key defining this field
-
template<typename
U
>
constexpr booloperator<
(Key<K, U> const &other) const¶ Define sort order for Keys.
This must be expressed as
operator<
instead of std::less because only std::less<void> supports arguments of mixed types, and it cannot be specialized.- Return
equivalent to
this->getId() < other.getId()
- Warning
this comparison operator provides a strict weak ordering so long as
K
does, but is not consistent with equality. In particular, keys with the same value ofgetId()
but different types will be equivalent but not equal.- Parameters
other
: the key, possibly of a different type, to compare to
Related
-
template<typename
V
, typenameK
>
constexpr Key<K, V>makeKey
(K const &id)¶ Factory function for Key, to enable type parameter inference.
Provides the same exception safety as the copy-constructor of
K
.- Return
a key of the desired type
- Parameters
id
: the key ID to create.
Calling this function prevents you from having to explicitly name the key type:
auto key = makeKey<int>("foo");
-
template<typename
K
, typenameV
>
std::ostream &operator<<
(std::ostream &os, Key<K, V> const &key)¶ Output operator for Key.
The output will use C++ template notation for the key; for example, a key “foo” pointing to an
int
may print as"foo<int>"
.Provides basic exception safety if the output operator of
K
is exception-safe.- Return
a reference to
os
- Parameters
os
: the desired output streamkey
: the key to print
- Warning
the type name is compiler-specific and may be mangled or unintuitive; for example, some compilers say “i” instead of “int”