fork download
  1. #include <vector>
  2.  
  3. template <typename T, typename U>
  4. struct is_same
  5. {
  6. enum { value = 0 };
  7. };
  8.  
  9. template <typename T>
  10. struct is_same<T, T>
  11. {
  12. enum { value = 1 };
  13. };
  14.  
  15. template <bool, typename>
  16. struct enable_if
  17. {};
  18.  
  19. template <typename T>
  20. struct enable_if<true, T>
  21. {
  22. typedef T type;
  23. };
  24.  
  25. template <typename T>
  26. struct ReadOnlyIterator
  27. {
  28. ReadOnlyIterator() {}
  29.  
  30. template <typename Container>
  31. ReadOnlyIterator(const Container &v, typename enable_if<is_same<T, typename Container::value_type>::value, void>::type * = 0) {}
  32.  
  33. template <typename Container>
  34. typename enable_if<is_same<T, typename Container::value_type>::value, ReadOnlyIterator<T>&>::type operator= (const Container &v) { return *this; }
  35. };
  36.  
  37. int main()
  38. {
  39. ReadOnlyIterator<int> i;
  40. std::vector<int> v;
  41. std::vector<short> w;
  42. i = v;
  43. //i = w; //uncomment for error
  44. ReadOnlyIterator<int> i2 = v;
  45. //ReadOnlyIterator<int> j = w; //uncomment for error
  46. }
Success #stdin #stdout 0s 2848KB
stdin
Standard input is empty
stdout
Standard output is empty