Skip to content

[BUG] Factory functions declared noexcept do not work with py::init() when compiling with C++17/C++20 #2666

Closed

Description

@dkavolis

Issue description

A modified example from the docs with noexcept factory function will not compile.

error C2027: use of undefined type 'pybind11::detail::initimpl::factory<Func,pybind11::detail::void_type (__cdecl *)(void),init::Example (int) noexcept,pybind11::detail::void_type (void)>'
        with
        [
            Func=init::Example (__cdecl *)(int) noexcept
        ]

Reproducible example code

class Example {
   private:
    Example(int) noexcept; // private constructor
   public:
    // Factory function:
    static Example create(int a) noexcept { return Example(a); }
  };

  py::class_<Example>(m, "Example")
      .def(py::init(&Example::create));

Activity

  1. bstaletic commented on Nov 16, 2020

    @bstaletic
    Collaborator

    I can't repro with gcc or clang. Does py::init([](int a) { return Example::create(a); }) work?

  2. changed the title [-][BUG] Factory functions declared noexcept do not work with py::init()[/-] [+][BUG] Factory functions declared noexcept do not work with py::init() when compiling with MSVC[/+] on Nov 16, 2020
  3. dkavolis commented on Nov 16, 2020

    @dkavolis
    Author

    I have only tried MSVC. And yes, either removing noexcept from the factory function or passing a lambda without noexcept works. Adding noexcept to either fails to compile. It's as if MSVC treats noexcept as part of the type.

    py::class_<...>.def(...) is unaffected by the noexcept specifier.

  4. ikicic commented on Nov 17, 2020

    @ikicic

    Could it be related to C++17? Compilation fails for me when using C++17 (g++ 7.3), but works when I either remove noexcept or switch to C++14.

    If I understand correctly, C++17 introduced pointers to noexcept functions, which are not covered by pybind's type traits (e.g. see remove_class here)

  5. dkavolis commented on Nov 17, 2020

    @dkavolis
    Author

    That must be it, it compiles with C++14 in MSVC as well.

  6. changed the title [-][BUG] Factory functions declared noexcept do not work with py::init() when compiling with MSVC[/-] [+][BUG] Factory functions declared noexcept do not work with py::init() when compiling with C++17/C++20[/+] on Nov 17, 2020
  7. bstaletic commented on Nov 17, 2020

    @bstaletic
    Collaborator

    Right. I don't know what I was testing, but I can confirm this issue. Anyway, this thing needs to be fixed. The paper that introduced this change in C++17 was P0012

  8. georgthegreat commented on May 17, 2023

    @georgthegreat

    I can confirm the issue.

    At the time python bindings for NVIDIA/TensorRT can not be compiler in post-c++17 mode.

  9. henryiii commented on Nov 12, 2026

    @henryiii
    Collaborator

    🤖 AI text below 🤖

    Fixed: py::init(&T::create) with a noexcept factory compiles in C++17 mode on current master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions