[C++-sig] newbie questions...

Ralf W. Grosse-Kunstleve rwgk at yahoo.com
Wed Mar 30 00:49:56 CEST 2005


--- Marco Correia <mvc at 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/ 



More information about the Cplusplus-sig mailing list