Test and document binding protected member functions

This commit is contained in:
Dean Moldovan
2017-08-17 17:03:46 +02:00
parent 9f6a636e54
commit 234f7c39a0
3 changed files with 146 additions and 1 deletions

View File

@@ -229,6 +229,57 @@ TEST_SUBMODULE(class_, m) {
// This test is actually part of test_local_bindings (test_duplicate_local), but we need a
// definition in a different compilation unit within the same module:
bind_local<LocalExternal, 17>(m, "LocalExternal", py::module_local());
// test_bind_protected_functions
class ProtectedA {
protected:
int foo() const { return value; }
private:
int value = 42;
};
class PublicistA : public ProtectedA {
public:
using ProtectedA::foo;
};
py::class_<ProtectedA>(m, "ProtectedA")
.def(py::init<>())
#if !defined(_MSC_VER) || _MSC_VER >= 1910
.def("foo", &PublicistA::foo);
#else
.def("foo", static_cast<int (ProtectedA::*)() const>(&PublicistA::foo));
#endif
class ProtectedB {
public:
virtual ~ProtectedB() = default;
protected:
virtual int foo() const { return value; }
private:
int value = 42;
};
class TrampolineB : public ProtectedB {
public:
int foo() const override { PYBIND11_OVERLOAD(int, ProtectedB, foo, ); }
};
class PublicistB : public ProtectedB {
public:
using ProtectedB::foo;
};
py::class_<ProtectedB, TrampolineB>(m, "ProtectedB")
.def(py::init<>())
#if !defined(_MSC_VER) || _MSC_VER >= 1910
.def("foo", &PublicistB::foo);
#else
.def("foo", static_cast<int (ProtectedB::*)() const>(&PublicistB::foo));
#endif
}
template <int N> class BreaksBase { public: virtual ~BreaksBase() = default; };