Say I have a class which is a wrapper around stl::list and I want to be able to use range looping:
#include <list>
using std::list;
template <typename T>
class list_wrapper
{
public:
list<T> _list;
typename list<T>::const_iterator begin() { return l.begin(); }
typename list<T>::const_iterator end() { return l.end(); }
typename list<T>::iterator begin() { return l.begin(); }
typename list<T>::iterator end() { return l.end(); }
};
int main(int argc, char** argv)
{
list_wrapper<int> myList;
// insert any number of elements into myList._list
/*
i want to be able to do:
for (auto& i : myList) { ... }
and
for (const auto& i : myList { ... }
*/
return 0;
}
It seems I can use iterator
s or const_iterator
s as begin()
and end()
cannot be overloaded, with the error message:
323070236/source.cpp:16:32: error: ‘typename std::__cxx11::list<T>::iterator wrap_list<T>::begin()’ cannot be overloaded
typename list<T>::iterator begin() { return l.begin(); }
^~~~~
323070236/source.cpp:14:38: error: with ‘typename std::__cxx11::list<T>::const_iterator wrap_list<T>::begin()’
typename list<T>::const_iterator begin() { return l.begin(); }
^~~~~
323070236/source.cpp:17:32: error: ‘typename std::__cxx11::list<T>::iterator wrap_list<T>::end()’ cannot be overloaded
typename list<T>::iterator end() { return l.end(); }
^~~
323070236/source.cpp:15:38: error: with ‘typename std::__cxx11::list<T>::const_iterator wrap_list<T>::end()’
typename list<T>::const_iterator end() { return l.end(); }
How would I be able to use both ::iterator
and ::const_iterator
here?
Aucun commentaire:
Enregistrer un commentaire