[C++-sig] Auto-converting to a custom string type?

Aaron Bentley abentley at panoramicfeedback.com
Mon Jul 18 17:45:53 CEST 2005

Our library uses a custom unicode string type called 'StringBuffer'.
I'd like to be able to auto-convert python unicode objects into
StringBuffers, because the alternative (AFAICT) it to wrap every
function that takes a StringBuffer parameter.

I thought I might be able to declare Python objects as implicitly
convertible to StringBuffer, but it doesn't quite work

StringBuffer make_StringBuffer(PyObject * uni_str)
   const char *utf8str = call_method<const char *>(uni_str, "encode",
   const char *end = utf8str + strlen(utf8str);
   StringBuffer sb;
   stringfunction::fromUTF8(sb.back_inserter(), utf8str, end);
   return sb;


def("make_StringBuffer", make_StringBuffer);

implicitly_convertible<PyObject *, StringBuffer>();

But what I get is

/home/abentley/develc/boost/boost/python/implicit.hpp:22:   instantiated
from `void boost::python::implicitly_convertible(boost::type<Target>*,
boost::type<U>*) [with Source = PyObject*, Target = StringBuffer]'
/home/abentley/develc/engine/python/pfengine.cpp:562:   instantiated
from here
/home/abentley/develc/boost/boost/python/converter/implicit.hpp:34: no
matching function for call to `StringBuffer::StringBuffer(PyObject*)'

It appears to insist that I provide a constructor
StringBuffer::StringBuffer(PyObject*), but I would rather do this
wrapping without altering the StringBuffer class.

Is there a better way?

Aaron Bentley
Director of Technology
Panometrics, Inc.

More information about the Cplusplus-sig mailing list