[forEach] Working with custom operator []

Hi i have a container class that defines 2 overloaded [] operators: T& operator[](const chat *); T& operator[](unsigned int); when i am using BOOST_FOREACH(T val,container) i get compilation errors but working directly with iterators is working ok, any idea?

From: avishay.ben.shabtai@gmail.com
Hi i have a container class that defines 2 overloaded [] operators: T& operator[](const chat *); T& operator[](unsigned int);
when i am using BOOST_FOREACH(T val,container)
i get compilation errors but working directly with iterators is working ok, any idea?
BOOST_FOREACH uses the begin() and end() member functions to get iterators pointing to the beginning and end of the range, then advances the begin iterator using ++ until it reaches the end. Along the way it dereferences the iterator to get the the element that is assigned to "val". It has nothing to do with operator[]. If you want your container to work with BOOST_FOREACH, you need to make sure it has begin() and end() member functions which return iterators with the appropriate capabilities (increment, dereference, equality comparison). If you're still having troubles, show us the compiler errors you get. Regards, Nate.

Nathan Ridge wrote:
From: avishay.ben.shabtai@gmail.com
Hi i have a container class that defines 2 overloaded [] operators: T& operator[](const chat *); T& operator[](unsigned int);
when i am using BOOST_FOREACH(T val,container)
i get compilation errors but working directly with iterators is working ok, any idea?
BOOST_FOREACH uses the begin() and end() member functions to get iterators pointing to the beginning and end of the range, then advances the begin iterator using ++ until it reaches the end. Along the way it dereferences the iterator to get the the element that is assigned to "val". It has nothing to do with operator[].
If you want your container to work with BOOST_FOREACH, you need to make sure it has begin() and end() member functions which return iterators with the appropriate capabilities (increment, dereference, equality comparison).
Or specialized boost::begin/boost::end perhaps? Jeff

Den 09-05-2011 14:26, Jeff Flinn skrev:
Nathan Ridge wrote:
If you want your container to work with BOOST_FOREACH, you need to make sure it has begin() and end() member functions which return iterators with the appropriate capabilities (increment, dereference, equality comparison).
Or specialized boost::begin/boost::end perhaps?
No ... overload range_begin()/range_end() in the namespace of the class. See the range docs. -Thorsten
participants (4)
-
Avishay Ben Shabtai
-
Jeff Flinn
-
Nathan Ridge
-
Thorsten Ottosen