Skip to content

Commit 91b42c8

Browse files
committed
Add upgrade guide entry about stricter compile-time checks
Closes #1048, closes #1052. [skip ci]
1 parent 1ad2227 commit 91b42c8

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

docs/upgrade.rst

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,33 @@ should be used directly instead: ``borrowed_t{}`` and ``stolen_t{}``
225225
(`#771 <https://github.com/pybind/pybind11/pull/771>`_).
226226

227227

228+
Stricter compile-time error checking
229+
------------------------------------
230+
231+
Some error checks have been moved from run time to compile time. Notably,
232+
automatic conversion of ``std::shared_ptr<T>`` is not possible when ``T`` is
233+
not directly registered with ``py::class_<T>`` (e.g. ``std::shared_ptr<int>``
234+
or ``std::shared_ptr<std::vector<T>>`` are not automatically convertible).
235+
Attempting to bind a function with such arguments now results in a compile-time
236+
error instead of waiting to fail at run time.
237+
238+
``py::init<...>()`` constructor definitions are also stricter and now prevent
239+
bindings which could cause unexpected behavior:
240+
241+
.. code-block:: cpp
242+
243+
struct Example {
244+
Example(int &);
245+
};
246+
247+
py::class_<Example>(m, "Example")
248+
.def(py::init<int &>()); // OK, exact match
249+
// .def(py::init<int>()); // compile-time error, mismatch
250+
251+
A non-``const`` lvalue reference is not allowed to bind to an rvalue. However,
252+
note that a constructor taking ``const T &`` can still be registered using
253+
``py::init<T>()`` because a ``const`` lvalue reference can bind to an rvalue.
254+
228255
v2.1
229256
====
230257

0 commit comments

Comments
 (0)