Is wrapping C++ library with ctypes a bad idea?

后端 未结 2 1293
眼角桃花
眼角桃花 2020-12-16 18:09

I read through the following two threads on wrapping C library and C++ library, I am not sure I get it yet. The C++ library I am working with does use class and template, bu

2条回答
  •  忘掉有多难
    2020-12-16 18:46

    In defence of boost::python, given Alexander's answer on ctypes:

    Boost python provides a very "c++" interface between c++ and python code - even doing things like allowed python subclasses of c++ classes to override virtual methods is relatively straightforward. Here's a potted list of good features:

    • Allow virtual methods of C++ classes to be overridden by python subclasses.
    • Bridge between std::vector<>, std::map<> instances and python lists and dictionaries (using vector_indexing_suite and map_indexing_suite)
    • Automatic sharing of reference counts in smart pointers (boost::shared_ptr, etc) with python reference counts (and you can extend this to any smart pointer).
    • Fine grained control of ownership when passing arguments and returning values from functions.

    Basically, if you have a design where you want to expose a c++ interface in a way faithful to the language, then boost::python is probably the best way to do it.

    The only downsides are increased compile time (boost::python makes extensive use of templates), and sometimes opaque error messages if you don't get things quite right.

提交回复
热议问题