BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdlb_transparentequalto.h
Go to the documentation of this file.
1/// @file bdlb_transparentequalto.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlb_transparentequalto.h -*-C++-*-
8#ifndef INCLUDED_BDLB_TRANSPARENTEQUALTO
9#define INCLUDED_BDLB_TRANSPARENTEQUALTO
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdlb_transparentequalto bdlb_transparentequalto
15/// @brief Provide a transparent equality predicate.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdlb
19/// @{
20/// @addtogroup bdlb_transparentequalto
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdlb_transparentequalto-purpose"> Purpose</a>
25/// * <a href="#bdlb_transparentequalto-classes"> Classes </a>
26/// * <a href="#bdlb_transparentequalto-description"> Description </a>
27/// * <a href="#bdlb_transparentequalto-usage"> Usage </a>
28/// * <a href="#bdlb_transparentequalto-example-1-basic-use-of-bdlb-transparentequalto"> Example 1: Basic Use of bdlb::TransparentEqualTo </a>
29///
30/// # Purpose {#bdlb_transparentequalto-purpose}
31/// Provide a transparent equality predicate.
32///
33/// # Classes {#bdlb_transparentequalto-classes}
34///
35/// - bdlb::TransparentEqualTo: a transparent equality predicate.
36///
37/// @see bdlb_transparentless, bdlb_transparenthash
38///
39/// # Description {#bdlb_transparentequalto-description}
40/// This component provides a `struct`, `bdlb::TransparentEqualTo`,
41/// that defines a functor that can be used as transparent equality comparator
42/// for heterogeneous lookup.
43///
44/// ## Usage {#bdlb_transparentequalto-usage}
45///
46///
47/// This section illustrates intended use of this component.
48///
49/// ### Example 1: Basic Use of bdlb::TransparentEqualTo {#bdlb_transparentequalto-example-1-basic-use-of-bdlb-transparentequalto}
50///
51///
52/// Suppose we need a container to store set of unique `bsl::string` objects.
53/// `bsl::unordered_set` is designed exactly for this purpose. But imagine that
54/// we want to use `bsl::string_view` objects for search operations within our
55/// container. `bsl::unordered_set` uses `bsl::equal_to` as default comparator.
56/// The problem is that even though the equality operator between `bsl::string`
57/// and `bsl::string_view` exists, the compiler will try to convert
58/// the `bsl::string_view` objects to `bsl::string` since `bsl::equal_to` is
59/// parameterized by `bsl::string`. The compilation will fail because there is
60/// no such implicit conversion. Even if the compilation were to succeed
61/// (because an implicit conversion were available), such an operation can lead
62/// to additional memory allocation for temporary objects. The following code
63/// illustrates how to use `bdlb::TransparentEqualTo` as a comparator for the
64/// standard container `unordered_set`, in this case to allow a
65/// `bsl::unordered_set<bsl::string>` to be searched with a `bsl::string_view`.
66///
67/// First, we create a container that uses `bdlb::TransparentEqualTo` as a
68/// comparator. Note that to avoid implicit conversions we also have to use
69/// transparent hash functor:
70/// @code
71/// typedef bsl::unordered_set<bsl::string,
72/// bdlb::TransparentHash,
73/// bdlb::TransparentEqualTo> TransparentSet;
74///
75/// TransparentSet transparentSet;
76/// @endcode
77/// Now, we fill the container with the strings:
78/// @code
79/// transparentSet.insert("NY");
80/// transparentSet.insert("LA");
81/// @endcode
82/// Finally, we observe that the container allows to use `bsl::string_view`
83/// objects as a key and does not make any implicit conversions:
84/// @code
85/// bsl::string_view newYork ("NY");
86/// bsl::string_view losAngeles ("LA");
87/// bsl::string_view sanFrancisco("SF");
88///
89/// assert(transparentSet.end() != transparentSet.find(newYork ));
90/// assert(transparentSet.end() != transparentSet.find(losAngeles ));
91/// assert(transparentSet.end() == transparentSet.find(sanFrancisco));
92/// @endcode
93/// @}
94/** @} */
95/** @} */
96
97/** @addtogroup bdl
98 * @{
99 */
100/** @addtogroup bdlb
101 * @{
102 */
103/** @addtogroup bdlb_transparentequalto
104 * @{
105 */
106
107#include <bdlscm_version.h>
108
112
113
114namespace bdlb {
115
116 // =========================
117 // struct TransparentEqualTo
118 // =========================
119
120/// This `struct` defines an equality of objects of different types,
121/// enabling them for use for heterogeneous comparison in the standard
122/// associative containers such as `bsl::unordered_map`. Note that this
123/// class is an empty POD type.
125
126 // TYPES
127
128 /// Type alias indicating this is a transparent comparator.
129 typedef void is_transparent;
130
131 // CREATORS
132
133 /// Create a `TransparentEqualTo` object.
135
136 /// Create a `TransparentEqualTo` object. Note that as
137 /// `TransparentEqualTo` is an empty (stateless) type, this operation
138 /// has no observable effect.
139 TransparentEqualTo(const TransparentEqualTo& original) = default;
140
141 /// Destroy this object.
143
144 // MANIPULATORS
145
146 /// Assign to this object the value of the specified `rhs` object, and
147 /// return a reference providing modifiable access to this object.
148 /// Note that as `TransparentEqualTo` is an empty (stateless) type, this
149 /// operation has no observable effect.
151
152 // ACCESSORS
153
154 /// Return `true` if the specified `lhs` is equal to the specified `rhs`
155 /// and `false` otherwise.
156 template <class LHS, class RHS>
157 bool operator()(const LHS& lhs, const RHS& rhs) const;
158};
159
160// ============================================================================
161// INLINE DEFINITIONS
162// ============================================================================
163
164 // -------------------------
165 // struct TransparentEqualTo
166 // -------------------------
167
168// ACCESSORS
169template <class LHS, class RHS>
170inline
171bool TransparentEqualTo::operator()(const LHS& lhs, const RHS& rhs) const
172{
173 return lhs == rhs;
174}
175
176} // close package namespace
177
178
179#endif
180
181// ----------------------------------------------------------------------------
182// Copyright 2021 Bloomberg Finance L.P.
183//
184// Licensed under the Apache License, Version 2.0 (the "License");
185// you may not use this file except in compliance with the License.
186// You may obtain a copy of the License at
187//
188// http://www.apache.org/licenses/LICENSE-2.0
189//
190// Unless required by applicable law or agreed to in writing, software
191// distributed under the License is distributed on an "AS IS" BASIS,
192// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
193// See the License for the specific language governing permissions and
194// limitations under the License.
195// ----------------------------- END-OF-FILE ----------------------------------
196
197/** @} */
198/** @} */
199/** @} */
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdlb_algorithmworkaroundutil.h:74
Definition bdlb_transparentequalto.h:124
void is_transparent
Type alias indicating this is a transparent comparator.
Definition bdlb_transparentequalto.h:129
TransparentEqualTo()=default
Create a TransparentEqualTo object.
bool operator()(const LHS &lhs, const RHS &rhs) const
Definition bdlb_transparentequalto.h:171
TransparentEqualTo(const TransparentEqualTo &original)=default
TransparentEqualTo & operator=(const TransparentEqualTo &rhs)=default
~TransparentEqualTo()=default
Destroy this object.