BDE 4.14.0 Production release
|
Provide a transparent less-than predicate.
This component provides a struct
, bdlb::TransparentLess
, that defines a functor that can be used as transparent less-than comparator for heterogeneous lookup.
This section illustrates intended use of this component.
Suppose we need a container to store set of unique bsl::strings
objects. We use bsl::set
for our container, which uses bsl::less
as default comparator. bsl::less
is suitable if we want to search an entry using a bsl::string
object as a key. However, if we were to try and search using a bsl::string_view
object the code would not compile, even though the comparison operator between bsl::string
and bsl::string_view
exists. In addition, implicit conversions where they are available, may lead to additional memory allocation for temporary objects. The following code illustrates how to use bdlb::TransparentLess
as a comparator for the standard container set
, in this case to allow a bsl::set<bsl::string>
to be searched with a bsl::string_view
.
First, we create several C-strings:
Next, we create several allocators to precisely control memory allocation:
And set one of them as default:
Then, we create two containers, one with default comparator and another using bdlb::TransparentLess
as a comparator:
Now, we fill the containers with the strings:
Finally, we observe that the container created with TransparentLess
container (userSet
) allows to use string_view object as a key and does not make any implicit conversions:
While the container using the default comparator implicitly converts C-strings to bsl::string
objects: