[C++-sig] arguments to c++ functions
Jim Bosch
talljimbo at gmail.com
Fri Aug 12 20:35:07 CEST 2011
On 08/09/2011 08:33 PM, Josh Stratton wrote:
> Where can I find docs for sending arguments to C\C++ functions? I've
> followed the hello world example, which seems to automatically convert
> a python string to a char*, but I'm going to need more conversions
> later like how to handle a char**. Something like ["a","b","c"]
> doesn't seem to work.
I don't think there are automatic conversions for char** - it can mean
too many things, and there's no automatic way to pass in the length.
If you want to pass in a sequence of Python strings, there are plenty of
ways to get C++ code to accept that. But if the C++ code you're dealing
with has char** arguments, you'll probably have to provide manual
wrappers for those functions; something like this:
namespace bp = boost::python;
void original(char** s, int n);
void wrapper(bp::object const & o) {
int n = bp::len(o);
std::vector<std::string> v(n);
boost::scoped_array<char*> s(new char*[n]);
for (int i = 0; i < n; ++i) {
v[i] = bp::extract<std::string>(o[i]);
s[i] = v[i].c_str();
}
original(s, n);
}
Good luck!
Jim Bosch
More information about the Cplusplus-sig
mailing list