Skip to content

Commit 481af06

Browse files
committed
Update docs to use override instead of overload where appropriate, and add warning about deprecated aliases
1 parent 6cc137d commit 481af06

File tree

5 files changed

+51
-42
lines changed

5 files changed

+51
-42
lines changed

docs/advanced/cast/custom.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ The following Python snippet demonstrates the intended usage from the Python sid
2929
from example import print
3030
print(A())
3131
32-
To register the necessary conversion routines, it is necessary to add
33-
a partial overload to the ``pybind11::detail::type_caster<T>`` template.
34-
Although this is an implementation detail, adding partial overloads to this
32+
To register the necessary conversion routines, it is necessary to add an
33+
instantiation of the ``pybind11::detail::type_caster<T>`` template.
34+
Although this is an implementation detail, adding an instantiation of this
3535
type is explicitly allowed.
3636

3737
.. code-block:: cpp

docs/advanced/cast/stl.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ the declaration
157157
158158
before any binding code (e.g. invocations to ``class_::def()``, etc.). This
159159
macro must be specified at the top level (and outside of any namespaces), since
160-
it instantiates a partial template overload. If your binding code consists of
160+
it adds a template instantiation of ``type_caster``. If your binding code consists of
161161
multiple compilation units, it must be present in every file (typically via a
162162
common header) preceding any usage of ``std::vector<int>``. Opaque types must
163163
also have a corresponding ``class_`` declaration to associate them with a name

docs/advanced/classes.rst

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ helper class that is defined as follows:
7171
7272
/* Trampoline (need one for each virtual function) */
7373
std::string go(int n_times) override {
74-
PYBIND11_OVERLOAD_PURE(
74+
PYBIND11_OVERRIDE_PURE(
7575
std::string, /* Return type */
7676
Animal, /* Parent class */
7777
go, /* Name of function in C++ (must match Python name) */
@@ -80,10 +80,10 @@ helper class that is defined as follows:
8080
}
8181
};
8282
83-
The macro :c:macro:`PYBIND11_OVERLOAD_PURE` should be used for pure virtual
84-
functions, and :c:macro:`PYBIND11_OVERLOAD` should be used for functions which have
83+
The macro :c:macro:`PYBIND11_OVERRIDE_PURE` should be used for pure virtual
84+
functions, and :c:macro:`PYBIND11_OVERRIDE` should be used for functions which have
8585
a default implementation. There are also two alternate macros
86-
:c:macro:`PYBIND11_OVERLOAD_PURE_NAME` and :c:macro:`PYBIND11_OVERLOAD_NAME` which
86+
:c:macro:`PYBIND11_OVERRIDE_PURE_NAME` and :c:macro:`PYBIND11_OVERRIDE_NAME` which
8787
take a string-valued name argument between the *Parent class* and *Name of the
8888
function* slots, which defines the name of function in Python. This is required
8989
when the C++ and Python versions of the
@@ -122,7 +122,7 @@ Bindings should be made against the actual class, not the trampoline helper clas
122122
123123
Note, however, that the above is sufficient for allowing python classes to
124124
extend ``Animal``, but not ``Dog``: see :ref:`virtual_and_inheritance` for the
125-
necessary steps required to providing proper overload support for inherited
125+
necessary steps required to providing proper overriding support for inherited
126126
classes.
127127

128128
The Python session below shows how to override ``Animal::go`` and invoke it via
@@ -182,15 +182,24 @@ Please take a look at the :ref:`macro_notes` before using this feature.
182182

183183
- because in these cases there is no C++ variable to reference (the value
184184
is stored in the referenced Python variable), pybind11 provides one in
185-
the PYBIND11_OVERLOAD macros (when needed) with static storage duration.
186-
Note that this means that invoking the overloaded method on *any*
185+
the PYBIND11_OVERRIDE macros (when needed) with static storage duration.
186+
Note that this means that invoking the overridden method on *any*
187187
instance will change the referenced value stored in *all* instances of
188188
that type.
189189

190190
- Attempts to modify a non-const reference will not have the desired
191191
effect: it will change only the static cache variable, but this change
192192
will not propagate to underlying Python instance, and the change will be
193-
replaced the next time the overload is invoked.
193+
replaced the next time the override is invoked.
194+
195+
.. warning::
196+
197+
The :c:macro:`PYBIND11_OVERRIDE` and accompanying macros used to be called
198+
``PYBIND11_OVERLOAD`` up until pybind11 v2.5.0, and :func:`get_override`
199+
used to be called ``get_overload``. This naming was corrected and the older
200+
macro and function names have been deprecated, in order to reduce confusion
201+
with overloaded functions and methods and ``py::overload_cast`` (see
202+
:ref:`_classes`).
194203

195204
.. seealso::
196205

@@ -238,20 +247,20 @@ override the ``name()`` method):
238247
class PyAnimal : public Animal {
239248
public:
240249
using Animal::Animal; // Inherit constructors
241-
std::string go(int n_times) override { PYBIND11_OVERLOAD_PURE(std::string, Animal, go, n_times); }
242-
std::string name() override { PYBIND11_OVERLOAD(std::string, Animal, name, ); }
250+
std::string go(int n_times) override { PYBIND11_OVERRIDE_PURE(std::string, Animal, go, n_times); }
251+
std::string name() override { PYBIND11_OVERRIDE(std::string, Animal, name, ); }
243252
};
244253
class PyDog : public Dog {
245254
public:
246255
using Dog::Dog; // Inherit constructors
247-
std::string go(int n_times) override { PYBIND11_OVERLOAD(std::string, Dog, go, n_times); }
248-
std::string name() override { PYBIND11_OVERLOAD(std::string, Dog, name, ); }
249-
std::string bark() override { PYBIND11_OVERLOAD(std::string, Dog, bark, ); }
256+
std::string go(int n_times) override { PYBIND11_OVERRIDE(std::string, Dog, go, n_times); }
257+
std::string name() override { PYBIND11_OVERRIDE(std::string, Dog, name, ); }
258+
std::string bark() override { PYBIND11_OVERRIDE(std::string, Dog, bark, ); }
250259
};
251260
252261
.. note::
253262

254-
Note the trailing commas in the ``PYBIND11_OVERLOAD`` calls to ``name()``
263+
Note the trailing commas in the ``PYBIND11_OVERIDE`` calls to ``name()``
255264
and ``bark()``. These are needed to portably implement a trampoline for a
256265
function that does not take any arguments. For functions that take
257266
a nonzero number of arguments, the trailing comma must be omitted.
@@ -266,9 +275,9 @@ declare or override any virtual methods itself:
266275
class PyHusky : public Husky {
267276
public:
268277
using Husky::Husky; // Inherit constructors
269-
std::string go(int n_times) override { PYBIND11_OVERLOAD_PURE(std::string, Husky, go, n_times); }
270-
std::string name() override { PYBIND11_OVERLOAD(std::string, Husky, name, ); }
271-
std::string bark() override { PYBIND11_OVERLOAD(std::string, Husky, bark, ); }
278+
std::string go(int n_times) override { PYBIND11_OVERRIDE_PURE(std::string, Husky, go, n_times); }
279+
std::string name() override { PYBIND11_OVERRIDE(std::string, Husky, name, ); }
280+
std::string bark() override { PYBIND11_OVERRIDE(std::string, Husky, bark, ); }
272281
};
273282
274283
There is, however, a technique that can be used to avoid this duplication
@@ -281,15 +290,15 @@ follows:
281290
template <class AnimalBase = Animal> class PyAnimal : public AnimalBase {
282291
public:
283292
using AnimalBase::AnimalBase; // Inherit constructors
284-
std::string go(int n_times) override { PYBIND11_OVERLOAD_PURE(std::string, AnimalBase, go, n_times); }
285-
std::string name() override { PYBIND11_OVERLOAD(std::string, AnimalBase, name, ); }
293+
std::string go(int n_times) override { PYBIND11_OVERRIDE_PURE(std::string, AnimalBase, go, n_times); }
294+
std::string name() override { PYBIND11_OVERRIDE(std::string, AnimalBase, name, ); }
286295
};
287296
template <class DogBase = Dog> class PyDog : public PyAnimal<DogBase> {
288297
public:
289298
using PyAnimal<DogBase>::PyAnimal; // Inherit constructors
290299
// Override PyAnimal's pure virtual go() with a non-pure one:
291-
std::string go(int n_times) override { PYBIND11_OVERLOAD(std::string, DogBase, go, n_times); }
292-
std::string bark() override { PYBIND11_OVERLOAD(std::string, DogBase, bark, ); }
300+
std::string go(int n_times) override { PYBIND11_OVERRIDE(std::string, DogBase, go, n_times); }
301+
std::string bark() override { PYBIND11_OVERRIDE(std::string, DogBase, bark, ); }
293302
};
294303
295304
This technique has the advantage of requiring just one trampoline method to be
@@ -342,7 +351,7 @@ valid for the trampoline class but not the registered class. This is primarily
342351
for performance reasons: when the trampoline class is not needed for anything
343352
except virtual method dispatching, not initializing the trampoline class
344353
improves performance by avoiding needing to do a run-time check to see if the
345-
inheriting python instance has an overloaded method.
354+
inheriting python instance has an overridden method.
346355

347356
Sometimes, however, it is useful to always initialize a trampoline class as an
348357
intermediate class that does more than just handle virtual method dispatching.
@@ -373,7 +382,7 @@ references (See also :ref:`faq_reference_arguments`). Another way of solving
373382
this is to use the method body of the trampoline class to do conversions to the
374383
input and return of the Python method.
375384

376-
The main building block to do so is the :func:`get_overload`, this function
385+
The main building block to do so is the :func:`get_override`, this function
377386
allows retrieving a method implemented in Python from within the trampoline's
378387
methods. Consider for example a C++ method which has the signature
379388
``bool myMethod(int32_t& value)``, where the return indicates whether
@@ -385,10 +394,10 @@ Python side by allowing the Python function to return ``None`` or an ``int``:
385394
bool MyClass::myMethod(int32_t& value)
386395
{
387396
pybind11::gil_scoped_acquire gil; // Acquire the GIL while in this scope.
388-
// Try to look up the overloaded method on the Python side.
389-
pybind11::function overload = pybind11::get_overload(this, "myMethod");
390-
if (overload) { // method is found
391-
auto obj = overload(value); // Call the Python function.
397+
// Try to look up the overridden method on the Python side.
398+
pybind11::function override = pybind11::get_override(this, "myMethod");
399+
if (override) { // method is found
400+
auto obj = override(value); // Call the Python function.
392401
if (py::isinstance<py::int_>(obj)) { // check if it returned a Python integer type
393402
value = obj.cast<int32_t>(); // Cast it and assign it to the value.
394403
return true; // Return true; value should be used.
@@ -1065,7 +1074,7 @@ described trampoline:
10651074
10661075
class Trampoline : public A {
10671076
public:
1068-
int foo() const override { PYBIND11_OVERLOAD(int, A, foo, ); }
1077+
int foo() const override { PYBIND11_OVERRIDE(int, A, foo, ); }
10691078
};
10701079
10711080
class Publicist : public A {

docs/advanced/misc.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ General notes regarding convenience macros
77
==========================================
88

99
pybind11 provides a few convenience macros such as
10-
:func:`PYBIND11_DECLARE_HOLDER_TYPE` and ``PYBIND11_OVERLOAD_*``. Since these
10+
:func:`PYBIND11_DECLARE_HOLDER_TYPE` and ``PYBIND11_OVERRIDE_*``. Since these
1111
are "just" macros that are evaluated in the preprocessor (which has no concept
1212
of types), they *will* get confused by commas in a template argument; for
1313
example, consider:
1414

1515
.. code-block:: cpp
1616
17-
PYBIND11_OVERLOAD(MyReturnType<T1, T2>, Class<T3, T4>, func)
17+
PYBIND11_OVERRIDE(MyReturnType<T1, T2>, Class<T3, T4>, func)
1818
1919
The limitation of the C preprocessor interprets this as five arguments (with new
2020
arguments beginning after each comma) rather than three. To get around this,
@@ -26,10 +26,10 @@ using the ``PYBIND11_TYPE`` macro:
2626
// Version 1: using a type alias
2727
using ReturnType = MyReturnType<T1, T2>;
2828
using ClassType = Class<T3, T4>;
29-
PYBIND11_OVERLOAD(ReturnType, ClassType, func);
29+
PYBIND11_OVERRIDE(ReturnType, ClassType, func);
3030
3131
// Version 2: using the PYBIND11_TYPE macro:
32-
PYBIND11_OVERLOAD(PYBIND11_TYPE(MyReturnType<T1, T2>),
32+
PYBIND11_OVERRIDE(PYBIND11_TYPE(MyReturnType<T1, T2>),
3333
PYBIND11_TYPE(Class<T3, T4>), func)
3434
3535
The ``PYBIND11_MAKE_OPAQUE`` macro does *not* require the above workarounds.
@@ -59,7 +59,7 @@ could be realized as follows (important changes highlighted):
5959
/* Acquire GIL before calling Python code */
6060
py::gil_scoped_acquire acquire;
6161
62-
PYBIND11_OVERLOAD_PURE(
62+
PYBIND11_OVERRIDE_PURE(
6363
std::string, /* Return type */
6464
Animal, /* Parent class */
6565
go, /* Name of function */

docs/reference.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,15 @@ Inheritance
9191

9292
See :doc:`/classes` and :doc:`/advanced/classes` for more detail.
9393

94-
.. doxygendefine:: PYBIND11_OVERLOAD
94+
.. doxygendefine:: PYBIND11_OVERRIDE
9595

96-
.. doxygendefine:: PYBIND11_OVERLOAD_PURE
96+
.. doxygendefine:: PYBIND11_OVERRIDE_PURE
9797

98-
.. doxygendefine:: PYBIND11_OVERLOAD_NAME
98+
.. doxygendefine:: PYBIND11_OVERRIDE_NAME
9999

100-
.. doxygendefine:: PYBIND11_OVERLOAD_PURE_NAME
100+
.. doxygendefine:: PYBIND11_OVERRIDE_PURE_NAME
101101

102-
.. doxygenfunction:: get_overload
102+
.. doxygenfunction:: get_override
103103

104104
Exceptions
105105
==========

0 commit comments

Comments
 (0)