Skip to content

Commit bc62536

Browse files
committed
bugfix: allow noexcept lambdas in CPP17. Fix #4565
1 parent 66f12df commit bc62536

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

include/pybind11/detail/common.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,16 @@ template <typename C, typename R, typename... A>
754754
struct remove_class<R (C::*)(A...) const> {
755755
using type = R(A...);
756756
};
757-
757+
#ifdef __cpp_noexcept_function_type
758+
template <typename C, typename R, typename... A>
759+
struct remove_class<R (C::*)(A...) noexcept> {
760+
using type = R(A...);
761+
};
762+
template <typename C, typename R, typename... A>
763+
struct remove_class<R (C::*)(A...) const noexcept> {
764+
using type = R(A...);
765+
};
766+
#endif
758767
/// Helper template to strip away type modifiers
759768
template <typename T>
760769
struct intrinsic_type {

tests/test_constants_and_functions.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,14 @@ TEST_SUBMODULE(constants_and_functions, m) {
148148
py::arg_v("y", 42, "<the answer>"),
149149
py::arg_v("z", default_value));
150150
});
151+
152+
// test noexcept(true) lambda
153+
m.def("l1", [](void) noexcept(true) {
154+
try {
155+
} catch (py::error_already_set &eas) {
156+
eas.discard_as_unraisable(__func__);
157+
} catch (const std::exception &e) {
158+
}
159+
return 0;
160+
});
151161
}

tests/test_constants_and_functions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,7 @@ def __repr__(self):
5050
m.register_large_capture_with_invalid_arguments(m)
5151
with pytest.raises(RuntimeError):
5252
m.register_with_raising_repr(m, RaisingRepr())
53+
54+
55+
def test_noexcept_lambda():
56+
assert m.l1() == 0

0 commit comments

Comments
 (0)