blob: 899bba666f81cc369d85e998e099e551152ff644 [file] [log] [blame]
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -04001/*
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02002 tests/test_virtual_functions.cpp -- overriding virtual functions from Python
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -04003
4 Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
5
6 All rights reserved. Use of this source code is governed by a
7 BSD-style license that can be found in the LICENSE file.
8*/
9
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020010#include "pybind11_tests.h"
11#include "constructor_stats.h"
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -040012#include <pybind11/functional.h>
13
14/* This is an example class that we'll want to be able to extend from Python */
15class ExampleVirt {
16public:
Jason Rhinelander3f589372016-08-07 13:05:26 -040017 ExampleVirt(int state) : state(state) { print_created(this, state); }
18 ExampleVirt(const ExampleVirt &e) : state(e.state) { print_copy_created(this); }
19 ExampleVirt(ExampleVirt &&e) : state(e.state) { print_move_created(this); e.state = 0; }
20 ~ExampleVirt() { print_destroyed(this); }
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -040021
22 virtual int run(int value) {
Dean Moldovan81511be2016-09-07 00:50:10 +020023 py::print("Original implementation of "
Jason Rhinelander7dfb9322016-09-08 14:49:43 -040024 "ExampleVirt::run(state={}, value={}, str1={}, str2={})"_s.format(state, value, get_string1(), *get_string2()));
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -040025 return state + value;
26 }
27
28 virtual bool run_bool() = 0;
29 virtual void pure_virtual() = 0;
Jason Rhinelander7dfb9322016-09-08 14:49:43 -040030
31 // Returning a reference/pointer to a type converted from python (numbers, strings, etc.) is a
32 // bit trickier, because the actual int& or std::string& or whatever only exists temporarily, so
33 // we have to handle it specially in the trampoline class (see below).
34 virtual const std::string &get_string1() { return str1; }
35 virtual const std::string *get_string2() { return &str2; }
36
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -040037private:
38 int state;
Jason Rhinelander7dfb9322016-09-08 14:49:43 -040039 const std::string str1{"default1"}, str2{"default2"};
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -040040};
41
42/* This is a wrapper class that must be generated */
43class PyExampleVirt : public ExampleVirt {
44public:
45 using ExampleVirt::ExampleVirt; /* Inherit constructors */
46
Jason Rhinelander116d37c2016-09-08 14:47:05 -040047 int run(int value) override {
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -040048 /* Generate wrapping code that enables native function overloading */
49 PYBIND11_OVERLOAD(
50 int, /* Return type */
51 ExampleVirt, /* Parent class */
52 run, /* Name of function */
53 value /* Argument(s) */
54 );
55 }
56
Jason Rhinelander116d37c2016-09-08 14:47:05 -040057 bool run_bool() override {
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -040058 PYBIND11_OVERLOAD_PURE(
59 bool, /* Return type */
60 ExampleVirt, /* Parent class */
61 run_bool, /* Name of function */
62 /* This function has no arguments. The trailing comma
63 in the previous line is needed for some compilers */
64 );
65 }
66
Jason Rhinelander116d37c2016-09-08 14:47:05 -040067 void pure_virtual() override {
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -040068 PYBIND11_OVERLOAD_PURE(
69 void, /* Return type */
70 ExampleVirt, /* Parent class */
71 pure_virtual, /* Name of function */
72 /* This function has no arguments. The trailing comma
73 in the previous line is needed for some compilers */
74 );
75 }
Jason Rhinelander7dfb9322016-09-08 14:49:43 -040076
77 // We can return reference types for compatibility with C++ virtual interfaces that do so, but
78 // note they have some significant limitations (see the documentation).
79 const std::string &get_string1() override {
80 PYBIND11_OVERLOAD(
81 const std::string &, /* Return type */
82 ExampleVirt, /* Parent class */
83 get_string1, /* Name of function */
84 /* (no arguments) */
85 );
86 }
87
88 const std::string *get_string2() override {
89 PYBIND11_OVERLOAD(
90 const std::string *, /* Return type */
91 ExampleVirt, /* Parent class */
92 get_string2, /* Name of function */
93 /* (no arguments) */
94 );
95 }
96
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -040097};
98
Jason Rhinelandered148792016-07-21 21:31:05 -040099class NonCopyable {
100public:
Jason Rhinelander3f589372016-08-07 13:05:26 -0400101 NonCopyable(int a, int b) : value{new int(a*b)} { print_created(this, a, b); }
102 NonCopyable(NonCopyable &&o) { value = std::move(o.value); print_move_created(this); }
Jason Rhinelandered148792016-07-21 21:31:05 -0400103 NonCopyable(const NonCopyable &) = delete;
104 NonCopyable() = delete;
105 void operator=(const NonCopyable &) = delete;
106 void operator=(NonCopyable &&) = delete;
107 std::string get_value() const {
108 if (value) return std::to_string(*value); else return "(null)";
109 }
Jason Rhinelander3f589372016-08-07 13:05:26 -0400110 ~NonCopyable() { print_destroyed(this); }
Jason Rhinelandered148792016-07-21 21:31:05 -0400111
112private:
113 std::unique_ptr<int> value;
114};
115
116// This is like the above, but is both copy and movable. In effect this means it should get moved
117// when it is not referenced elsewhere, but copied if it is still referenced.
118class Movable {
119public:
Jason Rhinelander3f589372016-08-07 13:05:26 -0400120 Movable(int a, int b) : value{a+b} { print_created(this, a, b); }
121 Movable(const Movable &m) { value = m.value; print_copy_created(this); }
122 Movable(Movable &&m) { value = std::move(m.value); print_move_created(this); }
Dean Moldovan99dbdc12016-08-19 13:45:36 +0200123 std::string get_value() const { return std::to_string(value); }
Jason Rhinelander3f589372016-08-07 13:05:26 -0400124 ~Movable() { print_destroyed(this); }
Jason Rhinelandered148792016-07-21 21:31:05 -0400125private:
126 int value;
127};
128
129class NCVirt {
130public:
131 virtual NonCopyable get_noncopyable(int a, int b) { return NonCopyable(a, b); }
132 virtual Movable get_movable(int a, int b) = 0;
133
Dean Moldovan99dbdc12016-08-19 13:45:36 +0200134 std::string print_nc(int a, int b) { return get_noncopyable(a, b).get_value(); }
135 std::string print_movable(int a, int b) { return get_movable(a, b).get_value(); }
Jason Rhinelandered148792016-07-21 21:31:05 -0400136};
137class NCVirtTrampoline : public NCVirt {
Wenzel Jakob1ffce742016-08-25 01:43:33 +0200138#if !defined(__INTEL_COMPILER)
Jason Rhinelander116d37c2016-09-08 14:47:05 -0400139 NonCopyable get_noncopyable(int a, int b) override {
Jason Rhinelandered148792016-07-21 21:31:05 -0400140 PYBIND11_OVERLOAD(NonCopyable, NCVirt, get_noncopyable, a, b);
141 }
Wenzel Jakob1ffce742016-08-25 01:43:33 +0200142#endif
Jason Rhinelander116d37c2016-09-08 14:47:05 -0400143 Movable get_movable(int a, int b) override {
Jason Rhinelandered148792016-07-21 21:31:05 -0400144 PYBIND11_OVERLOAD_PURE(Movable, NCVirt, get_movable, a, b);
145 }
146};
147
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -0400148int runExampleVirt(ExampleVirt *ex, int value) {
149 return ex->run(value);
150}
151
152bool runExampleVirtBool(ExampleVirt* ex) {
153 return ex->run_bool();
154}
155
156void runExampleVirtVirtual(ExampleVirt *ex) {
157 ex->pure_virtual();
158}
159
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400160
Jason Rhinelanderd6c365b2016-08-05 17:44:28 -0400161// Inheriting virtual methods. We do two versions here: the repeat-everything version and the
162// templated trampoline versions mentioned in docs/advanced.rst.
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400163//
Jason Rhinelanderd6c365b2016-08-05 17:44:28 -0400164// These base classes are exactly the same, but we technically need distinct
165// classes for this example code because we need to be able to bind them
166// properly (pybind11, sensibly, doesn't allow us to bind the same C++ class to
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400167// multiple python classes).
168class A_Repeat {
169#define A_METHODS \
170public: \
171 virtual int unlucky_number() = 0; \
Dean Moldovan99dbdc12016-08-19 13:45:36 +0200172 virtual std::string say_something(unsigned times) { \
173 std::string s = ""; \
174 for (unsigned i = 0; i < times; ++i) \
175 s += "hi"; \
176 return s; \
Jason Rhinelander20978262016-08-29 18:16:46 -0400177 } \
178 std::string say_everything() { \
179 return say_something(1) + " " + std::to_string(unlucky_number()); \
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400180 }
181A_METHODS
182};
183class B_Repeat : public A_Repeat {
184#define B_METHODS \
185public: \
186 int unlucky_number() override { return 13; } \
Dean Moldovan99dbdc12016-08-19 13:45:36 +0200187 std::string say_something(unsigned times) override { \
188 return "B says hi " + std::to_string(times) + " times"; \
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400189 } \
190 virtual double lucky_number() { return 7.0; }
191B_METHODS
192};
193class C_Repeat : public B_Repeat {
194#define C_METHODS \
195public: \
196 int unlucky_number() override { return 4444; } \
197 double lucky_number() override { return 888; }
198C_METHODS
199};
200class D_Repeat : public C_Repeat {
201#define D_METHODS // Nothing overridden.
202D_METHODS
203};
204
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400205// Base classes for templated inheritance trampolines. Identical to the repeat-everything version:
206class A_Tpl { A_METHODS };
207class B_Tpl : public A_Tpl { B_METHODS };
208class C_Tpl : public B_Tpl { C_METHODS };
209class D_Tpl : public C_Tpl { D_METHODS };
210
211
212// Inheritance approach 1: each trampoline gets every virtual method (11 in total)
213class PyA_Repeat : public A_Repeat {
214public:
215 using A_Repeat::A_Repeat;
216 int unlucky_number() override { PYBIND11_OVERLOAD_PURE(int, A_Repeat, unlucky_number, ); }
Dean Moldovan99dbdc12016-08-19 13:45:36 +0200217 std::string say_something(unsigned times) override { PYBIND11_OVERLOAD(std::string, A_Repeat, say_something, times); }
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400218};
219class PyB_Repeat : public B_Repeat {
220public:
221 using B_Repeat::B_Repeat;
222 int unlucky_number() override { PYBIND11_OVERLOAD(int, B_Repeat, unlucky_number, ); }
Dean Moldovan99dbdc12016-08-19 13:45:36 +0200223 std::string say_something(unsigned times) override { PYBIND11_OVERLOAD(std::string, B_Repeat, say_something, times); }
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400224 double lucky_number() override { PYBIND11_OVERLOAD(double, B_Repeat, lucky_number, ); }
225};
226class PyC_Repeat : public C_Repeat {
227public:
228 using C_Repeat::C_Repeat;
229 int unlucky_number() override { PYBIND11_OVERLOAD(int, C_Repeat, unlucky_number, ); }
Dean Moldovan99dbdc12016-08-19 13:45:36 +0200230 std::string say_something(unsigned times) override { PYBIND11_OVERLOAD(std::string, C_Repeat, say_something, times); }
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400231 double lucky_number() override { PYBIND11_OVERLOAD(double, C_Repeat, lucky_number, ); }
232};
233class PyD_Repeat : public D_Repeat {
234public:
235 using D_Repeat::D_Repeat;
236 int unlucky_number() override { PYBIND11_OVERLOAD(int, D_Repeat, unlucky_number, ); }
Dean Moldovan99dbdc12016-08-19 13:45:36 +0200237 std::string say_something(unsigned times) override { PYBIND11_OVERLOAD(std::string, D_Repeat, say_something, times); }
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400238 double lucky_number() override { PYBIND11_OVERLOAD(double, D_Repeat, lucky_number, ); }
239};
240
241// Inheritance approach 2: templated trampoline classes.
242//
243// Advantages:
244// - we have only 2 (template) class and 4 method declarations (one per virtual method, plus one for
245// any override of a pure virtual method), versus 4 classes and 6 methods (MI) or 4 classes and 11
246// methods (repeat).
247// - Compared to MI, we also don't have to change the non-trampoline inheritance to virtual, and can
248// properly inherit constructors.
249//
250// Disadvantage:
251// - the compiler must still generate and compile 14 different methods (more, even, than the 11
252// required for the repeat approach) instead of the 6 required for MI. (If there was no pure
253// method (or no pure method override), the number would drop down to the same 11 as the repeat
254// approach).
255template <class Base = A_Tpl>
256class PyA_Tpl : public Base {
257public:
258 using Base::Base; // Inherit constructors
259 int unlucky_number() override { PYBIND11_OVERLOAD_PURE(int, Base, unlucky_number, ); }
Dean Moldovan99dbdc12016-08-19 13:45:36 +0200260 std::string say_something(unsigned times) override { PYBIND11_OVERLOAD(std::string, Base, say_something, times); }
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400261};
262template <class Base = B_Tpl>
263class PyB_Tpl : public PyA_Tpl<Base> {
264public:
265 using PyA_Tpl<Base>::PyA_Tpl; // Inherit constructors (via PyA_Tpl's inherited constructors)
266 int unlucky_number() override { PYBIND11_OVERLOAD(int, Base, unlucky_number, ); }
Wenzel Jakob216df0d2016-08-12 00:59:57 +0200267 double lucky_number() override { PYBIND11_OVERLOAD(double, Base, lucky_number, ); }
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400268};
269// Since C_Tpl and D_Tpl don't declare any new virtual methods, we don't actually need these (we can
270// use PyB_Tpl<C_Tpl> and PyB_Tpl<D_Tpl> for the trampoline classes instead):
271/*
272template <class Base = C_Tpl> class PyC_Tpl : public PyB_Tpl<Base> {
273public:
274 using PyB_Tpl<Base>::PyB_Tpl;
275};
276template <class Base = D_Tpl> class PyD_Tpl : public PyC_Tpl<Base> {
277public:
278 using PyC_Tpl<Base>::PyC_Tpl;
279};
280*/
281
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400282
283void initialize_inherited_virtuals(py::module &m) {
284 // Method 1: repeat
Jason Rhinelander5fffe202016-09-06 12:17:06 -0400285 py::class_<A_Repeat, PyA_Repeat>(m, "A_Repeat")
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400286 .def(py::init<>())
287 .def("unlucky_number", &A_Repeat::unlucky_number)
Jason Rhinelander20978262016-08-29 18:16:46 -0400288 .def("say_something", &A_Repeat::say_something)
289 .def("say_everything", &A_Repeat::say_everything);
Jason Rhinelander6b52c832016-09-06 12:27:00 -0400290 py::class_<B_Repeat, A_Repeat, PyB_Repeat>(m, "B_Repeat")
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400291 .def(py::init<>())
292 .def("lucky_number", &B_Repeat::lucky_number);
Jason Rhinelander6b52c832016-09-06 12:27:00 -0400293 py::class_<C_Repeat, B_Repeat, PyC_Repeat>(m, "C_Repeat")
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400294 .def(py::init<>());
Jason Rhinelander6b52c832016-09-06 12:27:00 -0400295 py::class_<D_Repeat, C_Repeat, PyD_Repeat>(m, "D_Repeat")
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400296 .def(py::init<>());
297
298 // Method 2: Templated trampolines
Jason Rhinelander5fffe202016-09-06 12:17:06 -0400299 py::class_<A_Tpl, PyA_Tpl<>>(m, "A_Tpl")
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400300 .def(py::init<>())
301 .def("unlucky_number", &A_Tpl::unlucky_number)
Jason Rhinelander20978262016-08-29 18:16:46 -0400302 .def("say_something", &A_Tpl::say_something)
303 .def("say_everything", &A_Tpl::say_everything);
Jason Rhinelander6b52c832016-09-06 12:27:00 -0400304 py::class_<B_Tpl, A_Tpl, PyB_Tpl<>>(m, "B_Tpl")
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400305 .def(py::init<>())
306 .def("lucky_number", &B_Tpl::lucky_number);
Jason Rhinelander6b52c832016-09-06 12:27:00 -0400307 py::class_<C_Tpl, B_Tpl, PyB_Tpl<C_Tpl>>(m, "C_Tpl")
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400308 .def(py::init<>());
Jason Rhinelander6b52c832016-09-06 12:27:00 -0400309 py::class_<D_Tpl, C_Tpl, PyB_Tpl<D_Tpl>>(m, "D_Tpl")
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400310 .def(py::init<>());
311
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400312};
313
Dean Moldovanbdfb50f2017-06-07 16:52:50 +0200314struct Base {
315 /* for some reason MSVC2015 can't compile this if the function is pure virtual */
316 virtual std::string dispatch() const { return {}; };
317};
318
319struct DispatchIssue : Base {
320 virtual std::string dispatch() const {
321 PYBIND11_OVERLOAD_PURE(std::string, Base, dispatch, /* no arguments */);
322 }
323};
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400324
Dean Moldovan0bc272b2017-06-22 23:42:11 +0200325TEST_SUBMODULE(virtual_functions, m) {
Jason Rhinelander5fffe202016-09-06 12:17:06 -0400326 py::class_<ExampleVirt, PyExampleVirt>(m, "ExampleVirt")
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -0400327 .def(py::init<int>())
328 /* Reference original class in function definitions */
329 .def("run", &ExampleVirt::run)
330 .def("run_bool", &ExampleVirt::run_bool)
331 .def("pure_virtual", &ExampleVirt::pure_virtual);
332
Jason Rhinelandered148792016-07-21 21:31:05 -0400333 py::class_<NonCopyable>(m, "NonCopyable")
Wenzel Jakob1ffce742016-08-25 01:43:33 +0200334 .def(py::init<int, int>());
335
Jason Rhinelandered148792016-07-21 21:31:05 -0400336 py::class_<Movable>(m, "Movable")
Wenzel Jakob1ffce742016-08-25 01:43:33 +0200337 .def(py::init<int, int>());
338
339#if !defined(__INTEL_COMPILER)
Jason Rhinelander5fffe202016-09-06 12:17:06 -0400340 py::class_<NCVirt, NCVirtTrampoline>(m, "NCVirt")
Jason Rhinelandered148792016-07-21 21:31:05 -0400341 .def(py::init<>())
342 .def("get_noncopyable", &NCVirt::get_noncopyable)
343 .def("get_movable", &NCVirt::get_movable)
344 .def("print_nc", &NCVirt::print_nc)
Wenzel Jakob1ffce742016-08-25 01:43:33 +0200345 .def("print_movable", &NCVirt::print_movable);
346#endif
Jason Rhinelandered148792016-07-21 21:31:05 -0400347
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -0400348 m.def("runExampleVirt", &runExampleVirt);
349 m.def("runExampleVirtBool", &runExampleVirtBool);
350 m.def("runExampleVirtVirtual", &runExampleVirtVirtual);
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400351
Jason Rhinelander3f589372016-08-07 13:05:26 -0400352 m.def("cstats_debug", &ConstructorStats::get<ExampleVirt>);
Jason Rhinelander0ca96e22016-08-05 17:02:33 -0400353 initialize_inherited_virtuals(m);
Dean Moldovanbdfb50f2017-06-07 16:52:50 +0200354
Dean Moldovan0bc272b2017-06-22 23:42:11 +0200355 // test_alias_delay_initialization1
356 // don't invoke Python dispatch classes by default when instantiating C++ classes
357 // that were not extended on the Python side
358 struct A {
359 virtual ~A() {}
360 virtual void f() { py::print("A.f()"); }
361 };
362
363 struct PyA : A {
364 PyA() { py::print("PyA.PyA()"); }
365 ~PyA() { py::print("PyA.~PyA()"); }
366
367 void f() override {
368 py::print("PyA.f()");
369 PYBIND11_OVERLOAD(void, A, f);
370 }
371 };
372
373 py::class_<A, PyA>(m, "A")
374 .def(py::init<>())
375 .def("f", &A::f);
376
377 m.def("call_f", [](A *a) { a->f(); });
378
379 // test_alias_delay_initialization2
380 // ... unless we explicitly request it, as in this example:
381 struct A2 {
382 virtual ~A2() {}
383 virtual void f() { py::print("A2.f()"); }
384 };
385
386 struct PyA2 : A2 {
387 PyA2() { py::print("PyA2.PyA2()"); }
388 ~PyA2() { py::print("PyA2.~PyA2()"); }
389 void f() override {
390 py::print("PyA2.f()");
391 PYBIND11_OVERLOAD(void, A2, f);
392 }
393 };
394
395 py::class_<A2, PyA2>(m, "A2")
396 .def(py::init_alias<>())
397 .def("f", &A2::f);
398
399 m.def("call_f", [](A2 *a2) { a2->f(); });
400
Dean Moldovanbdfb50f2017-06-07 16:52:50 +0200401 // #159: virtual function dispatch has problems with similar-named functions
402 py::class_<Base, DispatchIssue>(m, "DispatchIssue")
403 .def(py::init<>())
404 .def("dispatch", &Base::dispatch);
405
406 m.def("dispatch_issue_go", [](const Base * b) { return b->dispatch(); });
407
408 // #392/397: overridding reference-returning functions
409 class OverrideTest {
410 public:
411 struct A { std::string value = "hi"; };
412 std::string v;
413 A a;
414 explicit OverrideTest(const std::string &v) : v{v} {}
415 virtual std::string str_value() { return v; }
416 virtual std::string &str_ref() { return v; }
417 virtual A A_value() { return a; }
418 virtual A &A_ref() { return a; }
419 };
420
421 class PyOverrideTest : public OverrideTest {
422 public:
423 using OverrideTest::OverrideTest;
424 std::string str_value() override { PYBIND11_OVERLOAD(std::string, OverrideTest, str_value); }
425 // Not allowed (uncommenting should hit a static_assert failure): we can't get a reference
426 // to a python numeric value, since we only copy values in the numeric type caster:
427// std::string &str_ref() override { PYBIND11_OVERLOAD(std::string &, OverrideTest, str_ref); }
428 // But we can work around it like this:
429 private:
430 std::string _tmp;
431 std::string str_ref_helper() { PYBIND11_OVERLOAD(std::string, OverrideTest, str_ref); }
432 public:
433 std::string &str_ref() override { return _tmp = str_ref_helper(); }
434
435 A A_value() override { PYBIND11_OVERLOAD(A, OverrideTest, A_value); }
436 A &A_ref() override { PYBIND11_OVERLOAD(A &, OverrideTest, A_ref); }
437 };
438
439 py::class_<OverrideTest::A>(m, "OverrideTest_A")
440 .def_readwrite("value", &OverrideTest::A::value);
441 py::class_<OverrideTest, PyOverrideTest>(m, "OverrideTest")
442 .def(py::init<const std::string &>())
443 .def("str_value", &OverrideTest::str_value)
444// .def("str_ref", &OverrideTest::str_ref)
445 .def("A_value", &OverrideTest::A_value)
446 .def("A_ref", &OverrideTest::A_ref);
Dean Moldovan0bc272b2017-06-22 23:42:11 +0200447}