mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-11 08:03:55 +00:00
docs: the order of alternatives for variant types matters, and follows the same rules as overload resolution (#2784)
This commit is contained in:
parent
40931961e3
commit
1faf4a8ae4
@ -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``
|
||||
|
@ -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
|
||||
=========================
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user