fork(2) download
  1. #include <algorithm>
  2. #include <vector>
  3. #include <array>
  4. #include <iostream>
  5.  
  6. const struct in_op final
  7. {
  8. template<typename T>
  9. struct impl
  10. {
  11. T const& x;
  12.  
  13. template<typename Container>
  14. bool operator>(Container const& c) const
  15. {
  16. return std::find(std::begin(c), std::end(c), x) != std::end(c);
  17. }
  18. };
  19.  
  20. template<typename T>
  21. friend auto operator<(T const &x, in_op) {
  22. return impl<T>{x};
  23. }
  24. } in;
  25.  
  26. int main()
  27. {
  28. const std::array<int, 3> a{ 1, 2, 3 };
  29. const std::vector<int> b{ 1, 2, 3 };
  30. const std::initializer_list<int> c{ 5, 6, 7 };
  31.  
  32. std::cout << "5 in { 1, 2, 3 } = " << std::boolalpha << (5 <in> a) << '\n';
  33. std::cout << "2 in { 1, 2, 3 } = " << std::boolalpha << (2 <in> b) << '\n';
  34. std::cout << "7 in { 5, 6, 7 } = " << std::boolalpha << (7 <in> c) << '\n';
  35. return 0;
  36. }
Success #stdin #stdout 0s 4544KB
stdin
Standard input is empty
stdout
5 in { 1, 2, 3 } = false
2 in { 1, 2, 3 } = true
7 in { 5, 6, 7 } = true