fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm> // std::remove_if
  4. using namespace std;
  5.  
  6. template <typename T>
  7. class check {
  8. public:
  9. virtual bool operator()(T x) = 0;
  10. };
  11.  
  12. template <typename T>
  13. struct cheat {
  14. cheat(check<T> *c) {_c = c;}
  15. bool operator()(T x) {return _c->operator ()(x);}
  16. check<T> *_c;
  17. };
  18.  
  19. int main() {
  20.  
  21. int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  22. std::vector<int> v( array, array+10 );
  23.  
  24. class even : public check<int>
  25. {
  26. public:
  27. virtual bool operator()( int x ) { cout<<"Hi"<<endl; return !( x % 2 ); }
  28. };
  29. even e;
  30.  
  31. remove_if( v.begin(), v.end(), cheat<int>(&e)); // no error
  32.  
  33. return 0;
  34. }
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
Hi
Hi
Hi
Hi
Hi
Hi
Hi
Hi
Hi
Hi