Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Jan 29th, 2013  |  syntax: C++  |  size: 2.20 KB  |  views: 41  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. flyrev@flyrev-VirtualBox:~/Desktop$ cat test.cpp
  2. #include <vector>
  3.  
  4. template <class T>
  5. class wrapped_vector {
  6. private:
  7.         std::vector<T> elements;
  8. public:
  9.         wrapped_vector() {
  10.                 elements.resize(20);
  11.         }
  12.  
  13.         typename std::vector<T>::reference operator[](typename std::vector<T>::size_type i) {
  14.                 return elements[i];
  15.         }
  16.  
  17.         typename std::vector<T>::const_reference operator[](int i) const {
  18.                 return elements[i];
  19.         }
  20. };
  21.  
  22. int main(void) {
  23.         wrapped_vector<int> test_int;
  24.         test_int[0] = 1;
  25.  
  26.         wrapped_vector<bool> test_bool;
  27.         test_bool[0] = true;
  28. }
  29. flyrev@flyrev-VirtualBox:~/Desktop$ g++ test.cpp
  30. test.cpp: In function ‘int main():
  31. test.cpp:23:12: warning: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second: [enabled by default]
  32. test.cpp:16:43: note: candidate 1: typename std::vector<T>::const_reference wrapped_vector<T>::operator[](int) const [with T = int; typename std::vector<T>::const_reference = const int&]
  33. test.cpp:12:37: note: candidate 2: typename std::vector<T>::reference wrapped_vector<T>::operator[](typename std::vector<T>::size_type) [with T = int; typename std::vector<T>::reference = int&; typename std::vector<T>::size_type = long unsigned int]
  34. test.cpp:23:16: error: assignment of read-only location ‘test_int.wrapped_vector<T>::operator[]<int>(0)
  35. test.cpp:26:13: warning: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second: [enabled by default]
  36. test.cpp:16:43: note: candidate 1: typename std::vector<T>::const_reference wrapped_vector<T>::operator[](int) const [with T = bool; typename std::vector<T>::const_reference = bool]
  37. test.cpp:12:37: note: candidate 2: typename std::vector<T>::reference wrapped_vector<T>::operator[](typename std::vector<T>::size_type) [with T = bool; typename std::vector<T>::reference = std::_Bit_reference; typename std::vector<T>::size_type = long unsigned int]
  38. test.cpp:26:17: error: lvalue required as left operand of assignment
  39. flyrev@flyrev-VirtualBox:~/Desktop$