Skip to content

Instantly share code, notes, and snippets.

@yoya
Created November 1, 2018 13:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yoya/acd8ac29c4e86a618c2a41be03350acb to your computer and use it in GitHub Desktop.
Save yoya/acd8ac29c4e86a618c2a41be03350acb to your computer and use it in GitHub Desktop.
iterator_test.cpp (NG)
// g++ -std=c++11 iterator_test.cpp
#include <iostream>
#include <vector>
#include <algorithm>
class my_vector {
public:
std::vector<int> m_vec;
my_vector(int len, int value) {
std::vector<int> v(len, value);
m_vec = std::move(v);
}
std::vector<int>::const_iterator begin() const { return m_vec.begin(); }
std::vector<int>::const_iterator end() const { return m_vec.end(); }
};
int main(void) {
const my_vector v(10, 255);
std::transform(v.begin(), v.end(), v.begin(),
[](float val) { return val/10; });
for (int n : v.m_vec) {
std::cout << n << ' ';
}
return 0;
}
@yoya
Copy link
Author

yoya commented Nov 1, 2018

% g++-8  -std=c++11 iterator_test.cpp
In file included from /usr/local/Cellar/gcc/8.2.0/include/c++/8.2.0/algorithm:62,
                 from iterator_test.cpp:5:
/usr/local/Cellar/gcc/8.2.0/include/c++/8.2.0/bits/stl_algo.h: In instantiation of '_OIter std::transform(_IIter, _IIter, _OIter, _UnaryOperation) [with _IIter = __gnu_cxx::__normal_iterator<const int*, std::vector<int> >; _OIter = __gnu_cxx::__normal_iterator<const int*, std::vector<int> >; _UnaryOperation = main()::<lambda(float)>]':
iterator_test.cpp:21:38:   required from here
/usr/local/Cellar/gcc/8.2.0/include/c++/8.2.0/bits/stl_algo.h:4304:12: error: assignment of read-only location '__result.__gnu_cxx::__normal_iterator<const int*, std::vector<int> >::operator*()'
  *__result = __unary_op(*__first);
  ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment