diff options
author | Yannick Jadoul <yannick.jadoul@belgacom.net> | 2021-01-14 05:17:27 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-13 23:17:27 -0500 |
commit | 1faf4a8ae4a1ef23c12a6f34291eebb11504c642 (patch) | |
tree | e35cc1d45e9749c749a499f323d88c928af9c58e | |
parent | 40931961e3983c23c4d23f02a3cbd281d808390d (diff) | |
download | pybind11-1faf4a8ae4a1ef23c12a6f34291eebb11504c642.tar.gz |
docs: the order of alternatives for variant types matters, and follows the same rules as overload resolution (#2784)
-rw-r--r-- | docs/advanced/cast/stl.rst | 11 | ||||
-rw-r--r-- | docs/advanced/functions.rst | 2 |
2 files changed, 13 insertions, 0 deletions
diff --git a/docs/advanced/cast/stl.rst b/docs/advanced/cast/stl.rst index 70fde0d2..b8622ee0 100644 --- a/docs/advanced/cast/stl.rst +++ b/docs/advanced/cast/stl.rst @@ -72,6 +72,17 @@ The ``visit_helper`` specialization is not required if your ``name::variant`` pr a ``name::visit()`` function. For any other function name, the specialization must be included to tell pybind11 how to visit the variant. +.. warning:: + + When converting a ``variant`` type, pybind11 follows the same rules as when + determining which function overload to call (:ref:`overload_resolution`), and + so the same caveats hold. In particular, the order in which the ``variant``'s + alternatives are listed is important, since pybind11 will try conversions in + this order. This means that, for example, when converting ``variant<int, bool>``, + the ``bool`` variant will never be selected, as any Python ``bool`` is already + an ``int`` and is convertible to a C++ ``int``. Changing the order of alternatives + (and using ``variant<bool, int>``, in this example) provides a solution. + .. note:: pybind11 only supports the modern implementation of ``boost::variant`` diff --git a/docs/advanced/functions.rst b/docs/advanced/functions.rst index ebdff9c9..55a40a5b 100644 --- a/docs/advanced/functions.rst +++ b/docs/advanced/functions.rst @@ -524,6 +524,8 @@ The default behaviour when the tag is unspecified is to allow ``None``. not allow ``None`` as argument. To pass optional argument of these copied types consider using ``std::optional<T>`` +.. _overload_resolution: + Overload resolution order ========================= |