Repository navigation
Closed
Labels
Description
Required prerequisites
- Make sure you've read the documentation. Your issue may be addressed there.
- Search the issue tracker and Discussions to verify that this hasn't already been reported. +1 or comment there if it has.
- Consider asking first in the Gitter chat room or in a Discussion.
What version (or hash if on master) of pybind11 are you using?
2.10.3
Problem description
When trying to add lambda with noexcept, compiling fails.
Begin from the code in documentation
#include <pybind11/pybind11.h>
namespace py = pybind11;
int nonthrowing_func() noexcept {
try
{
}
catch (py::error_already_set &eas)
{
eas.discard_as_unraisable(__func__);
}
catch (const std::exception &e)
{
}
return 0;
}
PYBIND11_MODULE(m, m)
{
m.def("a", nonthrowing_func);
}
this works fine.
However, if the function is turned into lambda,
#include <pybind11/pybind11.h>
namespace py = pybind11;
PYBIND11_MODULE(m, m)
{
m.def("b", [](void) noexcept(true) {
try
{
}
catch (py::error_already_set &eas)
{
eas.discard_as_unraisable(__func__);
}
catch(const std::exception& e)
{
}
return 0;
});
}
the compiler would complain as following:
[1/2] Building CXX object CMakeFiles/m.dir/export.cpp.o
FAILED: CMakeFiles/m.dir/export.cpp.o
/usr/bin/c++ -Dm_EXPORTS -isystem /usr/include/python3.10 -fPIC -fvisibility=hidden -flto -fno-fat-lto-objects -MD -MT CMakeFiles/m.dir/export.cpp.o -MF CMakeFiles/m.dir/export.cpp.o.d -o CMakeFiles/m.dir/export.cpp.o -c /home/kaigu/code/exact_solution/test_lambda/export.cpp
In file included from /usr/include/pybind11/attr.h:13,
from /usr/include/pybind11/detail/class.h:12,
from /usr/include/pybind11/pybind11.h:13,
from /home/kaigu/code/exact_solution/test_lambda/export.cpp:1:
/usr/include/pybind11/detail/common.h: In instantiation of ‘struct pybind11::detail::strip_function_object<pybind11_init_m(pybind11::module_&)::<lambda()> >’:
/usr/include/pybind11/detail/common.h:872:7: required by substitution of ‘template<class Function, class F> using function_signature_t = std::conditional_t<std::is_function<_NestedType>::value, F, typename std::conditional<(std::is_pointer<_T2>::value || std::is_member_pointer<F>::value), std::remove_pointer<F>, pybind11::detail::strip_function_object<F> >::type::type> [with Function = pybind11_init_m(pybind11::module_&)::<lambda()>; F = pybind11_init_m(pybind11::module_&)::<lambda()>]’
/usr/include/pybind11/pybind11.h:101:36: required from ‘pybind11::cpp_function::cpp_function(Func&&, const Extra& ...) [with Func = pybind11_init_m(pybind11::module_&)::<lambda()>; Extra = {pybind11::name, pybind11::scope, pybind11::sibling}; <template-parameter-1-3> = void]’
/usr/include/pybind11/pybind11.h:1153:22: required from ‘pybind11::module_& pybind11::module_::def(const char*, Func&&, const Extra& ...) [with Func = pybind11_init_m(pybind11::module_&)::<lambda()>; Extra = {}]’
/home/kaigu/code/exact_solution/test_lambda/export.cpp:7:7: required from here
/usr/include/pybind11/detail/common.h:867:11: error: no type named ‘type’ in ‘struct pybind11::detail::remove_class<int (pybind11_init_m(pybind11::module_&)::<lambda()>::*)() const noexcept>’
867 | using type = typename remove_class<decltype(&F::operator())>::type;
| ^~~~
ninja: build stopped: subcommand failed.
Changing noexcept(true) to noexcept does not help, but removing it would fix the issue.
The CMakeLists.txt is simple too:
cmake_minimum_required(VERSION 3.4)
project(example LANGUAGES CXX)
find_package(pybind11 REQUIRED CONFIG)
message(STATUS "Found pybind11 version ${pybind11_VERSION}")
pybind11_add_module(m "export.cpp")
Reproducible example code
See above
Is this a regression? Put the last known working version here if it is.
Not a regression
Activity
Skylion007 commentedon Mar 14, 2023
This seems related to #2234. It requires some better C++ templating that we have not gotten around to doing for C++17. Happy to accept a PR to fix it though if you are interested in contributing.
Skylion007 commentedon Mar 14, 2023
Seems like this template needs some additional overloads for noexcept:
pybind11/include/pybind11/detail/common.h
Line 748 in 442261d
Skylion007 commentedon Mar 14, 2023
Another option is to just strip the noexcept using a complicated template code like so: https://stackoverflow.com/a/55701361/2444240
Skylion007 commentedon Mar 16, 2023
@kaigu1997 We would be open to accepting a PR if someone wants to tackle this.
bugfix: allow noexcept lambdas in CPP17. Fix pybind#4565
bugfix: allow noexcept lambdas in CPP17. Fix pybind#4565