gpt4 book ai didi

python - boost python的指针参数

转载 作者:太空狗 更新时间:2023-10-30 01:17:38 25 4
gpt4 key购买 nike

使一个以指针作为参数的函数与 boost python 一起工作的最佳方法是什么?我看到文档中有很多返回值的可能性,但我不知道如何使用参数来实现。

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*)
>>>

最佳答案

据我所知,在对这个主题进行了一些谷歌搜索后,你不能。默认情况下,Python 不支持指针参数类型。如果您愿意,您可能可以手动编辑 Python 解释器,但在我看来这似乎是某种生产代码,所以这可能不是一个选项。

编辑:您可以像这样添加一个包装函数:

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

Then call your code with:


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


关于python - boost python的指针参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2385561/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com