Example on how two expose special functions to Python does not compile
- Dominant language
- HTML
- Stars
- 8.6k
- Forks
- 1.9k
- Avg merge
- 39m
- Merged PRs (30d)
- 2
Description
The [documentation](https://www.boost.org/doc/libs/1_86_0/libs/python/doc/html/tutorial/tutorial/exposing.html) contains many examples on how to use boost for python-c++ interoperation, but one of them does not compile:
```c++
#include
#include
using namespace boost::python;
using namespace std;
class Rational {
public:
operator double() const;
};
Rational pow(Rational, Rational);
Rational abs(Rational);
ostream &operator<<(ostream &, Rational);
BOOST_PYTHON_MODULE(hello) {
class_("Rational")
.def(float_(self)) // __float__
.def(pow(self, other)) // __pow__
.def(abs(self)) // __abs__
.def(str(self)) // __str__
;
}
```
```
$ g++ -I/usr/include/python3.12 -fpermissive main.cpp -c
main.cpp: In function 'void init_module_hello()':
main.cpp:19:37: error: expected primary-expression before ')' token
19 | .def(pow(self, other)) // __pow__
| ^
```
Removing the `.def(pow(self, other)) // __pow__` changes the error:
``` $ g++ -I/usr/include/python3.12 -fpermissive main.cpp -c
In file included from /usr/include/boost/python/object_core.hpp:20,
from /usr/include/boost/python/args.hpp:22,
from /usr/include/boost/python.hpp:11,
from main.cpp:1:
/usr/include/boost/python/def_visitor.hpp: In instantiation of 'static void boost::python::def_visitor_access::visit(const V&, classT&) [with V = boost::python::def_visitor; classT = boost::python::class_]':
/usr/include/boost/python/def_visitor.hpp:67:34: required from 'void boost::python::def_visitor::visit(classT&) const [with classT = boost::python::class_; DerivedVisitor = boost::python::api::object]'
67 | def_visitor_access::visit(*this, c);
| ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~
/usr/include/boost/python/class.hpp:221:22: required from 'boost::python::class_::self& boost::python::class_::def(const boost::python::def_visitor&) [with Derived = boost::python::api::object; W = Rational; X1 = boost::python::detail::not_specified; X2 = boost::python::detail::not_specified; X3 = boost::python::detail::not_specified; self = boost::python::class_]'
221 | visitor.visit(*this);
| ~~~~~~~~~~~~~^~~~~~~
main.cpp:20:11: required from here
17 | class_("Rational")
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18 | .def(float_(self)) // __float__
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
19 | .def(abs(self)) // __abs__
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 | .def(str(self)) // __str__
| ~~~~^~~~~~~~~~~
/usr/include/boost/python/def_visitor.hpp:31:34: error: no matching function for call to 'boost::python::api::object::visit(boost::python::class_&) const'
31 | v.derived_visitor().visit(c);
| ~~~~~~~~~~~~~~~~~~~~~~~~~^~~
/usr/include/boost/python/object_core.hpp:160:12: note: candidate: 'template void boost::python::api::object_operators::visit(ClassT&, const char*, const boost::python::detail::def_helper&) const [with DocStringT = ClassT; U = boost::python::api::object]'
160 | void visit(ClassT& cl, char const* name, python::detail::def_helper const& helper) const
| ^~~~~
/usr/include/boost/python/object_core.hpp:160:12: note: candidate expects 3 arguments, 1 provided
```
I use boost 1.86.0 and gcc 14.2.1, both installed using the Arch Linux package manager. Adding `--std=c++11`, `--std=c++14`, `--std=c++17` or `--std=c++20` does not solve the issue.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.