[C++-sig] Raw constructor (i.e. combination of make_constructor and raw_function)

Ralf W. Grosse-Kunstleve rwgk at yahoo.com
Thu Aug 25 02:07:54 CEST 2005


--- Hans Meine <hans_meine at gmx.net> wrote:
> v1 = createVector(1,2,3)

I recommend against doing it that way. It is not compatible with the C++
interface:

std::vector<double> a(1, 2);

initializes a vector of size 1 with a value of 2.

This looks and works best in my experience:

  vector([1,2,3])

Here is a full wrapper which works that way:

http://cvs.sourceforge.net/viewcvs.py/cctbx/scitbx/include/scitbx/stl/vector_wrapper.h?view=markup

There are two tricks:

1. Also use scitbx/boost_python/container_conversions.h to define automatic
Python list/tuple/range -> std::vector<> mappings:

      scitbx::boost_python::container_conversions::from_python_sequence<
        w_t,
        scitbx::boost_python::container_conversions
          ::variable_capacity_policy>();

where w_t is the "wrapped type", e.g. std::vector<double>.

2. Wrap the copy constructor:

        .def(init<w_t const&>())

Note that the vector_wrapper also supports pickling.

Cheers,
        Ralf


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 



More information about the Cplusplus-sig mailing list