Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The answer was actually quite simple: make the container behave like an stl container, and then to use boost's <code>iterator&lt;&gt;()</code> function instead of <code>range()</code>.</p> <p>To do this, I had to make the typedefs in <code>stl_iter</code> public, and to rename them to <code>value_type</code> and <code>iterator</code>.</p> <p>Heres the updated C++ code.</p> <pre><code>#include &lt;memory&gt; #include &lt;vector&gt; #include &lt;string&gt; #include &lt;boost/python.hpp&gt; template&lt;class T&gt; T* get_pointer(std::shared_ptr&lt;T&gt; const&amp; p) { return p.get(); } template&lt;class T&gt; class stl_iter { protected: virtual T&amp; get_vector()=0; public: // Next two lines changed, and made public typedef typename T::value_type value_type; typedef typename T::iterator iterator; virtual ~stl_iter() {} virtual void append(value_type item) { get_vector().push_back(item); } virtual iterator begin() { return get_vector().begin(); } virtual iterator end() { return get_vector().end(); } }; class K { std::string val; public: K(std::string s) : val(s) {} std::string get_val() const { return val; } }; typedef std::shared_ptr&lt;K&gt; pK; typedef std::vector&lt;pK&gt; vK; class Container : public stl_iter&lt;vK&gt; { vK items; protected: vK&amp; get_vector() { return items; } public: virtual ~Container() {} }; typedef std::shared_ptr&lt;Container&gt; pContainer; typedef std::vector&lt;pContainer&gt; vContainer; BOOST_PYTHON_MODULE_INIT(test) { using namespace boost::python; class_&lt;K, pK&gt;("K", init&lt;std::string&gt;()) .def("__str__", &amp;K::get_val) ; class_&lt;Container, pContainer&gt;("Container") .def("append", &amp;Container::append) // Use iterator() instead of range() .def("__iter__", iterator&lt;Container&gt;()) ; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload