Skip to content

[BUG]: Unable to Add Lambda with noexcept #4565

Closed

Description

@kaigu1997

Required prerequisites

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

  1. added
    triage
    on Mar 13, 2023
  2. added and removed
    triage
    on Mar 14, 2023
  3. Skylion007 commented on Mar 14, 2023

    @Skylion007
    Collaborator

    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.

  4. Skylion007 commented on Mar 14, 2023

    @Skylion007
    Collaborator

    Seems like this template needs some additional overloads for noexcept:

    struct remove_class {};
    or we need to add a template that strips out noexcept from method signatures in C++17 as discussed in the issue.

  5. Skylion007 commented on Mar 14, 2023

    @Skylion007
    Collaborator

    Another option is to just strip the noexcept using a complicated template code like so: https://stackoverflow.com/a/55701361/2444240

  6. Skylion007 commented on Mar 16, 2023

    @Skylion007
    Collaborator

    @kaigu1997 We would be open to accepting a PR if someone wants to tackle this.

  7. added 2 commits that reference this issue on Mar 27, 2023
    350f472
    bc62536
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions