1

I want std::set if shared_ptr's to compare the pointee's, not pointers.
I have this example:

std::shared_ptr<std::string> s(new std::string("abc"));
std::shared_ptr<std::string> p(new std::string("abc"));
std::set<std::shared_ptr<std::string>> S;
S.insert(s);
S.insert(p);
std::cout << S.size();

As you can see I am putting the same element in the set but this outputs 2.
How can I make set's insert to use the comparison criteria of underlying strings? And what if its not a string but more complex object?

1

1 Answer 1

5

The second template parameter of std::set is the type of the comparator to be used (default is std::less<Key>):

#include <iostream>
#include <memory>
#include <set>
#include <string>

struct deref_less {
  bool operator()(const auto& a, const auto& b) const { return (*a) < (*b); }
  using is_transparent = void;
};

int main() {
  std::shared_ptr<std::string> s(new std::string("abc"));
  std::shared_ptr<std::string> p(new std::string("abc"));
  std::set<std::shared_ptr<std::string>, deref_less> S;
  S.insert(s);
  S.insert(p);
  std::cout << S.size();
}

Output:

1

auto parameters for convenience with C++20, before the comparator is a bit more verbose. using is_transparent = void; to enable eg the set::find overload that accepts a std::unique_ptr<std::string> (see godbolt example).

5
  • "Missing" using is_transparent = void; :-)
    – Jarod42
    Mar 10, 2021 at 11:52
  • 2
    compare_shared is not the best name (compare tends to return negative, null, positive), how about deref_less?
    – Jarod42
    Mar 10, 2021 at 11:54
  • @Jarod42 didnt like the name either, thanks for the suggestion. I don't quite get the purpose of is_transparent yet. Found the templated overload for std::set::find that takes some param of type K that can be compared to Keys, but then do I not have to provide a std::string < std::shared_ptr<std::string> comparison? Mar 10, 2021 at 11:58
  • Here, you might compare shared_ptr with unique_ptr with raw pointer. std::less<void> is transparent but you cannot compare anything with anything. So you don't have to provide T* < T.
    – Jarod42
    Mar 10, 2021 at 12:50
  • @Jarod42 got it. Thanks. Mar 10, 2021 at 20:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.