fork download
  1. #include <iostream>
  2. #include <type_traits>
  3. #include <vector>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. template<class T> struct container {
  8. vector<T> ts;
  9. template<class F> auto contains(F&& f) const ->
  10. typename enable_if< is_convertible< decltype(f(declval<T>())), bool >::value, bool >::type
  11. {
  12. cout << "predicate\n";
  13. return find_if(begin(ts), end(ts), f) != end(ts);
  14. }
  15. template<class V> auto contains(V&& v) const ->
  16. typename enable_if< is_convertible< decltype(declval<T>() == v), bool >::value, bool >::type
  17. {
  18. cout << "match\n";
  19. return find(begin(ts), end(ts), v) != end(ts);
  20. }
  21. };
  22.  
  23. struct XZ { void xz() {} };
  24. typedef void(XZ::*unspecified_bool)();
  25.  
  26. unspecified_bool unspec(bool v) { return v ? &XZ::xz : (unspecified_bool)0; }
  27.  
  28. struct ONE {};
  29. unspecified_bool operator == (int x, ONE y) {
  30. cout << "compare " << x << " to ONE...\n";
  31. return unspec(x == 1);
  32. }
  33.  
  34.  
  35. int main() {
  36. container<int> d { {0, 3, 1, 2} };
  37. cout << d.contains([](double x) {
  38. cout << "examine " << x << "...\n";
  39. return unspec(x==1); })
  40. << endl;
  41. cout << d.contains(ONE()) << endl;
  42. }
  43.  
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
predicate
examine 0...
examine 3...
examine 1...
1
match
compare 0 to ONE...
compare 3 to ONE...
compare 1 to ONE...
1