开发者

Pointer argument to boost python

开发者 https://www.devze.com 2022-12-21 20:44 出处:网络
What\'s th开发者_如何转开发e best way to make a function that has pointer as argument work with boost python?

What's th开发者_如何转开发e best way to make a function that has pointer as argument work with boost python? I see there are many possibilities for return values in the docs, but I don't know how to do it with arguments.

void Tesuto::testp(std::string* s)
{
    if (!s)
        cout << " NULL s" << endl;
    else
        cout << s << endl;
}

>>> t.testp(None)
 NULL s
>>>       
>>> s='test'
>>> t.testp(s)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
Boost.Python.ArgumentError: Python argument types in
    Tesuto.testp(Tesuto, str)
did not match C++ signature:
    testp(Tesuto {lvalue}, std::string*)
>>>                        


As far as I can tell, after doing a bit of googling on the subject, is that you can't. Python doesn't support pointer argument types by default. If you wanted to, you could probably edit the python interpreter by hand, but this seems to me to be production code of some sort, so that probably isn't an option.

EDIT: You could add a wrapper function like so:

 
std::string * pointer (std::string& p)
{
    return &p;
}

Then call your code with:


>>> s = 'hello'
>>> t.testp (pointer (s))
hello
>>>

0

精彩评论

暂无评论...
验证码 换一张
取 消