Dear all,
thanks a lot for your immediate responses that lead to a fast solution for my 
previous problem.

Here comes the next one.

In the example below both classes A and B are derived from std::string. Class B 
can be implicitly converted to A by means of 'B::operator const A()'. So the 
function printme(const A&) can also be called with an argument of class B (see 
main function).

My aim is to be able to call in python:
>>>import boost_ext
>>>b=boost_ext.B("hello world")
>>>boost_ext.printme(b)
but it produces the error:
> <class 'Boost.Python.ArgumentError'>: Python argument types in  
> boost_ext.printme(B)
> did not match C++ signature:
>    printme(A)

Can I modify the declaration inside BOOST_PYTHON_MODULE so that the implicit 
conversion is recognized and the above call boost_ext.printme(b) succeeds?

Thank you
Mihail

BTW, in the code below I'm using a preprocessor definition __BJAM__ which I had 
to define explicitly in the Jamroot. Is there some variable defined by default 
which I can use to distinguish between bjam builds and other (testing) calls to 
g++?

boost.cpp:
#ifdef __BJAM__ // I define __BJAM__ in the Jamroot file..
  #include <boost/python.hpp>
#endif
#include <string>
#include <iostream>

class A: public std::string{
public:
  A(const std::string&s,int i):std::string(s){}
};

class B: public std::string{
public:
  B(const std::string&s):std::string(s){}
  operator const A(){return A(*this,-1);}
};

void printme(const A &a){std::cout<<a<<std::endl;}

int main(){
  B b("hello world");
  printme(b);
}

#ifdef __BJAM__
BOOST_PYTHON_MODULE(boost_ext)
{
    using namespace boost::python;
    class_<std::string>("string");
    class_<A,bases<std::string> > ("A",init<const std::string&,int>());
    class_<B,bases<std::string> > ("B",init<const std::string&>());
    def("printme",&printme);
}
#endif



      

_______________________________________________
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig

Reply via email to