Environment: Boost 1.61.0 compiled with Python 3.5
The following C++ code outputs 12:
class A
{
public:
int func() { return 12; }
};
BOOST_PYTHON_MODULE(bridge)
{
using namespace boost::python;
class_<A>("A", no_init)
.def("func", &A::func);
}
int main()
{
A a;
PyImport_AppendInittab("bridge", PyInit_bridge);
Py_Initialize();
using namespace boost::python;
dict dictMain = extract<dict>(import("__main__").attr("__dict__"));
import("bridge").attr("a") = boost::ref(a);
exec("import bridge", dictMain);
exec("print(bridge.a.func())", dictMain);
}
However, if I replace boost::ref with std::ref, a boost::python::error_already_set instance is thrown.
Why cannot std::ref be used here?
Nice article about handling python exceptions in C++. Any way I guess python through exception
AttributeErrorbecause of difference inreference_wrapperimplementations in std and boost libraries. You can see difference even in public interface.std::reference_wrapperhas noget_pointer()method.