BDE 4.14.0 Production release
|
Provide a transparent equality predicate.
This component provides a struct
, bdlb::TransparentEqualTo
, that defines a functor that can be used as transparent equality comparator for heterogeneous lookup.
This section illustrates intended use of this component.
Suppose we need a container to store set of unique bsl::string
objects. bsl::unordered_set
is designed exactly for this purpose. But imagine that we want to use bsl::string_view
objects for search operations within our container. bsl::unordered_set
uses bsl::equal_to
as default comparator. The problem is that even though the equality operator between bsl::string
and bsl::string_view
exists, the compiler will try to convert the bsl::string_view
objects to bsl::string
since bsl::equal_to
is parameterized by bsl::string
. The compilation will fail because there is no such implicit conversion. Even if the compilation were to succeed (because an implicit conversion were available), such an operation can lead to additional memory allocation for temporary objects. The following code illustrates how to use bdlb::TransparentEqualTo
as a comparator for the standard container unordered_set
, in this case to allow a bsl::unordered_set<bsl::string>
to be searched with a bsl::string_view
.
First, we create a container that uses bdlb::TransparentEqualTo
as a comparator. Note that to avoid implicit conversions we also have to use transparent hash functor:
Now, we fill the container with the strings:
Finally, we observe that the container allows to use bsl::string_view
objects as a key and does not make any implicit conversions: