[C++-sig] Re: implicitly_convertible and char*
Dusty Leary
dleary at gmail.com
Tue Jun 29 00:17:03 CEST 2004
full source code:
================================================
#define BOOST_PYTHON_STATIC_LIB
#include <boost/python.hpp>
using namespace boost::python;
struct X {
std::string v;
X(const std::string& s) : v(s) {}
operator char*() const { return (char*)v.c_str(); }
};
void test_x(X& x) {
char* x_string = x;
printf("test_x: %s\n", x_string);
}
void test_string(char* s) {
printf("test_string: %s\n", s);
}
BOOST_PYTHON_MODULE(test_string) {
class_<X>("X", init<const std::string&>());
def("test_x", test_x);
def("test_string", test_string);
implicitly_convertible<X, char*>();
}
================================================
interpreter:
>>> import test_string
>>> x = test_string.X("foo")
>>> test_string.test_x(x)
test_x: foo
>>> test_string.test_string("blah")
test_string: blah
>>> test_string.test_string(x)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
Boost.Python.ArgumentError: Python argument types in
test_string.test_string(X)
did not match C++ signature:
test_string(char *)
On Sun, 27 Jun 2004 20:39:56 -0400, David Abrahams
<dave at boost-consulting.com> wrote:
>
>
> Dusty Leary <dleary at gmail.com> writes:
>
> > The implicitly_convertible sample at
> > http://boost.org/libs/python/doc/v2/implicit.html#implicitly_convertible-spec
> > works fine for me.
> >
> > However, changing "int" to "char*" makes it stop working. Compiles
> > fine, won't invoke at runtime.
> >
> >>From the traceback:
> >
> > Boost.Python.ArgumentError: Python argument types in
> > __testmodule__.test_string(X)
> > did not match C++ signature:
> > test_string(char *)
> >
> > I am using MSVC7.1
> >
> > Any info?
>
> Full source code of your example?
> Perhaps the problem is that X isn't actually convertible to char*?
>
> --
> Dave Abrahams
> Boost Consulting
> http://www.boost-consulting.com
>
> _______________________________________________
> C++-sig mailing list
> C++-sig at python.org
> http://mail.python.org/mailman/listinfo/c++-sig
>
More information about the Cplusplus-sig
mailing list