<small id='6hfPO'></small><noframes id='6hfPO'>

  • <tfoot id='6hfPO'></tfoot>
      <bdo id='6hfPO'></bdo><ul id='6hfPO'></ul>

    <i id='6hfPO'><tr id='6hfPO'><dt id='6hfPO'><q id='6hfPO'><span id='6hfPO'><b id='6hfPO'><form id='6hfPO'><ins id='6hfPO'></ins><ul id='6hfPO'></ul><sub id='6hfPO'></sub></form><legend id='6hfPO'></legend><bdo id='6hfPO'><pre id='6hfPO'><center id='6hfPO'></center></pre></bdo></b><th id='6hfPO'></th></span></q></dt></tr></i><div id='6hfPO'><tfoot id='6hfPO'></tfoot><dl id='6hfPO'><fieldset id='6hfPO'></fieldset></dl></div>
    1. <legend id='6hfPO'><style id='6hfPO'><dir id='6hfPO'><q id='6hfPO'></q></dir></style></legend>

        Boost.Python call by reference:TypeError: No to_python (by-v

        时间:2023-07-20
        <tfoot id='AAEZp'></tfoot>
            <tbody id='AAEZp'></tbody>
            • <bdo id='AAEZp'></bdo><ul id='AAEZp'></ul>
            • <small id='AAEZp'></small><noframes id='AAEZp'>

              • <i id='AAEZp'><tr id='AAEZp'><dt id='AAEZp'><q id='AAEZp'><span id='AAEZp'><b id='AAEZp'><form id='AAEZp'><ins id='AAEZp'></ins><ul id='AAEZp'></ul><sub id='AAEZp'></sub></form><legend id='AAEZp'></legend><bdo id='AAEZp'><pre id='AAEZp'><center id='AAEZp'></center></pre></bdo></b><th id='AAEZp'></th></span></q></dt></tr></i><div id='AAEZp'><tfoot id='AAEZp'></tfoot><dl id='AAEZp'><fieldset id='AAEZp'></fieldset></dl></div>
                <legend id='AAEZp'><style id='AAEZp'><dir id='AAEZp'><q id='AAEZp'></q></dir></style></legend>

                1. 本文介绍了Boost.Python call by reference:TypeError: No to_python (by-value) converter found for C++ type:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试使用 Boost.Python 将我的 C++ 类公开给 Python.这是我正在尝试做的事情的简化版本:

                  I'm trying to expose my C++ Classes to Python using Boost.Python. Here is a simplyfied version of what i'm trying to do:

                  我有一个从 boost::noncopyable 派生的类 A 和第二个类 B,其方法将 A 的引用作为参数.

                  I have a class A deriving from boost::noncopyable and a second class B with a method that takes a reference to A as an argument.

                  class A : boost::noncopyable { /*...*/ };
                  
                  class B {
                  
                  public:
                  
                      virtual void do_something(A& a) {
                          /*...*/
                      }
                  };
                  

                  我公开的类如下:

                  /* Wrapper for B, so B can be extended in python */
                  struct BWrap : public B, wrapper<B> {
                  
                      void do_something(A &a) {
                  
                          if (override do_something = this->get_override("do_something")) {
                              do_something(a);
                              return;
                          }
                          else {
                              B::do_something(a);
                          }
                      }
                  
                      void default_do_something(A& a) { this->B::do_something(a); }
                  };
                  
                  BOOST_PYTHON_MODULE(SomeModule) {
                  
                      class_<A, boost::noncopyable>("A");
                  
                      class_<BWrap, boost::noncopyable>("B")
                          .def("do_something", &B::do_something, &BWrap::default_do_something)
                      ;
                  }
                  

                  我在 python 中扩展 B 是这样的:

                  I extend B in python like this:

                  test.py:

                  import SomeModule
                  
                  
                  class BDerived(SomeModule.B):
                  
                      def do_something(self, a):
                          pass
                  

                  并像这样调用扩展的 B:

                  and call the extended B like this:

                  try {
                      py::object main = py::import("__main__"); 
                      py::object global(main.attr("__dict__")); 
                      py::object result = py::exec_file("test.py", global, global); 
                      py::object pluginClass = global["BDerived"]; 
                      py::object plugin_base = pluginClass(); 
                  
                      B& plugin = py::extract<B&>(plugin_base) BOOST_EXTRACT_WORKAROUND;
                  
                      A a;
                      B.do_something(a);
                  }
                  catch (py::error_already_set) { 
                      PyErr_Print();
                  }
                  

                  但是这会导致错误消息:

                  However this results in an error message:

                  TypeError: No to_python (by-value) converter found for C++ type: A
                  

                  如果 A 不是从 boost::noncopyable 派生的,则代码运行没有任何错误,但是 do_something(A& a) 中的参数 acode> 在函数调用期间被复制,即使它是通过引用传入的.但是仅仅删除 A 上的不可复制要求不是一个选项,因为它存在是有原因的.

                  If A isn't derived from boost::noncopyable the code runs without any errors but the argument a in do_something(A& a) gets copied during the function call even though it's passed in by reference. But just removing the noncopyable requirement on A isn't an option since it's there for a reason.

                  有什么解决问题的建议吗?

                  Any suggestions how to solve the problem?

                  谢谢.

                  推荐答案

                  B.do_something(a); 更改为 B.do_something(boost::ref(a));.

                  参见 调用 Python 函数和方法 在 boost 手册中.

                  See Calling Python Functions and Methods in the boost manual.

                  这篇关于Boost.Python call by reference:TypeError: No to_python (by-value) converter found for C++ type:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  <legend id='DvC19'><style id='DvC19'><dir id='DvC19'><q id='DvC19'></q></dir></style></legend>

                  <tfoot id='DvC19'></tfoot>

                      <tbody id='DvC19'></tbody>
                  1. <small id='DvC19'></small><noframes id='DvC19'>

                        <bdo id='DvC19'></bdo><ul id='DvC19'></ul>

                        • <i id='DvC19'><tr id='DvC19'><dt id='DvC19'><q id='DvC19'><span id='DvC19'><b id='DvC19'><form id='DvC19'><ins id='DvC19'></ins><ul id='DvC19'></ul><sub id='DvC19'></sub></form><legend id='DvC19'></legend><bdo id='DvC19'><pre id='DvC19'><center id='DvC19'></center></pre></bdo></b><th id='DvC19'></th></span></q></dt></tr></i><div id='DvC19'><tfoot id='DvC19'></tfoot><dl id='DvC19'><fieldset id='DvC19'></fieldset></dl></div>