Re: [C++-sig] newbie questions...
--- Marco Correia <mvc@di.fct.unl.pt> wrote:
the std::pair<T1,T2> to a python tuple. I've learned that I cannot have bindings for c++ templates, so lets say that T1=T2=int.
Here you go (tested with g++ 3.2.3): #include <boost/python/module.hpp> #include <boost/python/def.hpp> #include <boost/python/tuple.hpp> #include <boost/python/to_python_converter.hpp> namespace { template <typename T1, typename T2> struct std_pair_to_tuple { static PyObject* convert(std::pair<T1,T2> const& p) { return boost::python::incref( boost::python::make_tuple(p.first, p.second).ptr()); } }; template <typename T1, typename T2> struct std_pair_to_python_converter { std_pair_to_python_converter() { boost::python::to_python_converter< std::pair<T1, T2>, std_pair_to_tuple<T1, T2> >(); } }; std::pair<int, int> foo() { std::pair<int, int> result; result.first = 3; result.second = 5; return result; } } // namespace anonymous BOOST_PYTHON_MODULE(std_pair_example) { using namespace boost::python; std_pair_to_python_converter<int, int>(); def("foo", foo); } __________________________________ Do you Yahoo!? Yahoo! Small Business - Try our new resources site! http://smallbusiness.yahoo.com/resources/
"Ralf W. Grosse-Kunstleve" <rwgk@yahoo.com> writes:
template <typename T1, typename T2> struct std_pair_to_python_converter { std_pair_to_python_converter() { boost::python::to_python_converter< std::pair<T1, T2>, std_pair_to_tuple<T1, T2> >(); } };
wouldn't template <typename T1, typename T2> void std_pair_to_python_converter() { boost::python::to_python_converter< std::pair<T1, T2>, std_pair_to_tuple<T1, T2> >(); } be so much simpler? -- Dave Abrahams Boost Consulting www.boost-consulting.com
participants (2)
-
David Abrahams -
Ralf W. Grosse-Kunstleve