
From: Neal D. Becker [mailto:ndbecker2@verizon.net] Sent: September 8, 2004 15:59
u.begin() should return an iterator, and the iterator should define operator[]. Why doesn't this work?
huh ? Which iterator defines operator[] ? It may define operator+(size_type) or similar, if it allows random access, but not operator[]. If you know your container manages contiguous memory you may use '&*u.begin()' to access the raw memory, and apply your operator[] to that. Regards, Stefan

Stefan Seefeld <sseefeld@art.ca> writes:
From: Neal D. Becker [mailto:ndbecker2@verizon.net] Sent: September 8, 2004 15:59
u.begin() should return an iterator, and the iterator should define operator[]. Why doesn't this work?
huh ? Which iterator defines operator[] ?
All legal random access iterators do (including, of course, pointers). But nobody should use an iterator's operator[] in generic code because it may be inefficient. -- Dave Abrahams Boost Consulting http://www.boost-consulting.com

Stefan Seefeld wrote:
From: Neal D. Becker [mailto:ndbecker2@verizon.net] Sent: September 8, 2004 15:59
u.begin() should return an iterator, and the iterator should define operator[]. Why doesn't this work?
huh ? Which iterator defines operator[] ? It may define operator+(size_type) or similar, if it allows random access, but not operator[]. If you know your container manages contiguous memory you may use '&*u.begin()' to access the raw memory, and apply your operator[] to that.
Consider this: #include <vector> #include <boost/numeric/ublas/vector.hpp> template<typename it_t> int adder (it_t in, int cnt) { int sum = 0; for (int i = 0; i < cnt; i++) sum += in[i]; return sum; } int main() { std::vector<int> a (10); adder (a.begin(), 10); << this is OK boost::numeric::ublas::vector<int> b; adder (b.begin(), 10); << error, why? } g++34 -I /usr/local/src/boost.cvs -c Test.cc Test.cc: In function `int adder(it_t, int) [with it_t = boost::numeric::ublas::vector<int, boost::numeric::ublas::unbounded_array<int, std::allocator<int> >
::iterator]': Test.cc:17: instantiated from here Test.cc:8: error: no match for 'operator[]' in 'in[i]'
participants (3)
-
David Abrahams
-
Neal Becker
-
Stefan Seefeld