BDE 4.39.x 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::TransparentStringHash,
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 associative containers such as `bsl::unordered_map`.
122///
123/// \note Note that this
124/// class is an empty POD type.
125///
126/// See @ref bdlb_transparentequalto
128
129 // TYPES
130
131 /// Type alias indicating this is a transparent comparator.
132 typedef void is_transparent;
133
134 // CREATORS
135
136 /// Create a `TransparentEqualTo` object.
138
139 /// Create a `TransparentEqualTo` object.
140 /// \note Note that as
141 /// `TransparentEqualTo` is an empty (stateless) type, this operation
142 /// has no observable effect.
143 TransparentEqualTo(const TransparentEqualTo& original) = default;
144
145 /// Destroy this object.
147
148 // MANIPULATORS
149
150 /// Assign to this object the value of the specified `rhs` object, and
151 /// return a reference providing modifiable access to this object.
152 ///
153 /// \note Note that as `TransparentEqualTo` is an empty (stateless) type, this
154 /// operation has no observable effect.
156
157 // ACCESSORS
158
159 /// Return `true` if the specified `lhs` is equal to the specified `rhs`
160 /// and `false` otherwise.
161 template <class LHS, class RHS>
162 bool operator()(const LHS& lhs, const RHS& rhs) const;
163};
164
165// ============================================================================
166// INLINE DEFINITIONS
167// ============================================================================
168
169 // -------------------------
170 // struct TransparentEqualTo
171 // -------------------------
172
173// ACCESSORS
174template <class LHS, class RHS>
175inline
176bool TransparentEqualTo::operator()(const LHS& lhs, const RHS& rhs) const
177{
178 return lhs == rhs;
179}
180
181} // close package namespace
182
183
184#endif
185
186// ----------------------------------------------------------------------------
187// Copyright 2021 Bloomberg Finance L.P.
188//
189// Licensed under the Apache License, Version 2.0 (the "License");
190// you may not use this file except in compliance with the License.
191// You may obtain a copy of the License at
192//
193// http://www.apache.org/licenses/LICENSE-2.0
194//
195// Unless required by applicable law or agreed to in writing, software
196// distributed under the License is distributed on an "AS IS" BASIS,
197// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
198// See the License for the specific language governing permissions and
199// limitations under the License.
200// ----------------------------- END-OF-FILE ----------------------------------
201
202/** @} */
203/** @} */
204/** @} */
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bdlb_algorithmworkaroundutil.h:74
Definition bdlb_transparentequalto.h:127
void is_transparent
Type alias indicating this is a transparent comparator.
Definition bdlb_transparentequalto.h:132
TransparentEqualTo()=default
Create a TransparentEqualTo object.
bool operator()(const LHS &lhs, const RHS &rhs) const
Definition bdlb_transparentequalto.h:176
TransparentEqualTo(const TransparentEqualTo &original)=default
TransparentEqualTo & operator=(const TransparentEqualTo &rhs)=default
~TransparentEqualTo()=default
Destroy this object.