[C++-sig] Can't convert to unsigned int

David Abrahams dave at boost-consulting.com
Fri Dec 13 23:03:43 CET 2002

"Langston, Matthew David" <langston at SLAC.Stanford.EDU> writes:

> I just ran into this same problem last night.
> In High Energy Physics we are always using unsigned longs as event bit
> masks. It would be nice if Python had a bit-vector type for this
> purpose, but since it doesn't (does it?) a Python long seems like the
> next best thing.
> Is there a way to do this on a case-by-case basis (i.e. convert this
> unsigned long to a Python long for this member function, but not that
> one). 

Sorry I never answered this.

You can use a thin wrapper which accepts or returns a
boost::python::long object when that's what you want to work with.

I realize that's a bit cumbersome, though.

> Barring that, I couldn't figure out a way to even register a
> global to_python_converter to convert all unsigned longs to Python
> longs. Is this even possible?

Not when going from C++ to Python.  Those conversions are handled by a
template specialization, not by anything which goes in the converter
registry :(.

I think I'm going to resolve this one by converting integers that are
out-of-range for Python ints into Python longs. It's slightly
nontrivial, but still doable.

                       David Abrahams
   dave at boost-consulting.com * http://www.boost-consulting.com
Boost support, enhancements, training, and commercial distribution

More information about the Cplusplus-sig mailing list