const_iterator type
Hi, I'm trying to wrap an iterable class, which returns a custom iterator typedef'd as const_iterator, from its begin and end methods. When using boost::python::(iterator|iterators|range) to create an iterator in the exposed __iter__ function in the exposed class_ def, I get a load of compile errors:- /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/stl_iterator_base_types.h:166:53: error: no type named 'iterator_category' in 'class mylib::MyConstIterator' /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/stl_iterator_base_types.h:167:53: error: no type named 'value_type' in 'class mylib::MyConstIterator' /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/stl_iterator_base_types.h:168:53: error: no type named 'difference_type' in 'class mylib::MyConstIterator' /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/stl_iterator_base_types.h:169:53: error: no type named 'pointer' in 'class mylib::MyConstIterator' /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/stl_iterator_base_types.h:170:53: error: no type named 'reference' in 'class mylib::MyConstIterator' Here's a simple example demonstrating the problem:- // const_iterator.cpp #include <boost/python/class.hpp> #include <boost/python/copy_const_reference.hpp> #include <boost/python/iterator.hpp> #include <boost/python/module.hpp> // External lib declaration // namespace notmylib { // forward declaration class MyConstIterator; class MyData { public: MyData(void); virtual ~MyData(void); // ... typedef MyConstIterator const_iterator; const_iterator begin(void) const; const_iterator end(void) const; }; class MyConstIterator { public: typedef std::pair<int, int> TRange; // constructors MyConstIterator(void); MyConstIterator(const MyData); // dtor ~MyConstIterator(void) {} // copy constructor MyConstIterator(const MyConstIterator& iter); // assignment operator MyConstIterator& operator= (const MyConstIterator& iter); // Comparison operators bool operator==(const MyConstIterator& iter) const; bool operator!=(const MyConstIterator& iter) const; // Get Data Range TRange GetRange(void) const; // Go backwards one step void Rewind(void); private: MyData m_data; size_t m_index; }; } BOOST_PYTHON_MODULE(const_iterator) { boost::python::class_<notmylib::MyData, boost::noncopyable>("Data") .def("__iter__", boost::python::iterator< const notmylib::MyData, boost::python::return_value_policy< boost::python::copy_const_reference> >() ) // ... ; } Although this doesn't have the member function definitions, so won't work as a complete example from Python, it completely replicates the compile error I get with the actual class I'm trying to wrap. Command to compile, using GCC 4.7.2:- g++ -c -L/usr/lib -g -O1 -fPIC -I/usr/include/python2.7 const_iterator.cpp -o const_iterator.o -lboost_python I couldn't find any examples using const_iterator in the test directory, or elsewhere, so can't find anything to go by. I thought to manually add those typedef's the compilers complaining about, to a Python wrapper class derived from MyIterator. Is there another class I can inherit from, in boost or the STL, to do this for me? Any alternative suggestions or recommendations would be appreciated! Kind regards, Alex --
On Wed, 27 Mar 2013 11:36:47 -0000, Alex Leach <beamesleach@gmail.com> wrote:
I'm trying to wrap an iterable class, which returns a custom iterator typedef'd as const_iterator, from its begin and end methods.
In case any of you like answering these types of questions on StackExchange sites, I've also posted it up on StackOverflow: http://stackoverflow.com/questions/15659610/how-to-expose-c-classes-with-con... Kind regards, Alex
I couldn't find any examples using const_iterator in the test directory, or elsewhere, so can't find anything to go by. I thought to manually add those typedef's the compilers complaining about, to a Python wrapper class derived from MyIterator. Is there another class I can inherit from, in boost or the STL, to do this for me?
Wy can't you add them to the iterator itself? That's where they belong.
The iterator I'm wrapping comes from a 3rd party library, over which I have no control. However, I have been able to add those missing types to the derived inheritor class, by also inheriting from 'iterator_facade', as per a suggestion on StackOverflow. Thanks for the reply btw; I have no clue when it comes to C++ standards-compliance, so all the tips and pointers are appreciated! Cheers, Alex On Thu, Mar 28, 2013 at 6:52 PM, Stefan Ring <stefanrin@gmail.com> wrote:
I couldn't find any examples using const_iterator in the test directory, or elsewhere, so can't find anything to go by. I thought to manually add those typedef's the compilers complaining about, to a Python wrapper class derived from MyIterator. Is there another class I can inherit from, in boost or the STL, to do this for me?
Wy can't you add them to the iterator itself? That's where they belong. _______________________________________________ Cplusplus-sig mailing list Cplusplus-sig@python.org http://mail.python.org/mailman/listinfo/cplusplus-sig
participants (2)
-
Alex Leach -
Stefan Ring