fork download
  1. #include <iostream>
  2. #include <set>
  3. #include <string>
  4. #include <type_traits>
  5. using namespace std;
  6.  
  7. struct base
  8. {
  9. struct compare
  10. {
  11. template <typename T, std::enable_if_t<!std::is_base_of<base, T>::value, bool> = true>
  12. bool operator()(const T& lhs, const base& rhs) const { return lhs < rhs.Name; }
  13.  
  14. template <typename T, std::enable_if_t<!std::is_base_of<base, T>::value, bool> = true>
  15. bool operator()(const base& lhs, const T& rhs) const { return lhs.Name < rhs; }
  16.  
  17. bool operator()(const base& lhs, const base& rhs) const { return lhs.Name < rhs.Name; }
  18.  
  19. using is_transparent = int;
  20. };
  21.  
  22. /*explicit base(const char * name) : Name(name) {} */
  23. explicit base(std::string name) : Name(std::move(name)) {}
  24.  
  25. virtual ~base() {};
  26.  
  27. std::string Name;
  28. };
  29. /*
  30. base::~base()
  31. {
  32. }
  33. */
  34. struct derived : base
  35. {
  36. explicit derived(const std::string& name)
  37. : base(name)
  38. {
  39. }
  40. };
  41.  
  42. int main()
  43. {
  44. std::set<derived, base::compare> baseset;
  45.  
  46. baseset.emplace("Test");
  47.  
  48. auto x = baseset.find("Test");
  49.  
  50. std::cout << (x != baseset.end() ? x->Name : "not found!") << std::endl;
  51.  
  52. auto y = baseset.find(string("Test2"));
  53.  
  54. std::cout << (y != baseset.end() ? y->Name : "not found!") << std::endl;
  55. }
  56.  
Success #stdin #stdout 0.01s 5424KB
stdin
Standard input is empty
stdout
Test
not found!