You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[BUG] Factory functions declared noexcept do not work with py::init() when compiling with C++17/C++20 #2666
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
classExample {
private:Example(int) noexcept; // private constructorpublic:// Factory function:static Example create(int a) noexcept { returnExample(a); }
};
py::class_<Example>(m, "Example")
.def(py::init(&Example::create));
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
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.
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
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
Activity
bstaletic commentedon Nov 16, 2020
I can't repro with gcc or clang. Does
py::init([](int a) { return Example::create(a); })work?[-][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[/+]dkavolis commentedon Nov 16, 2020
I have only tried MSVC. And yes, either removing
noexceptfrom the factory function or passing a lambda withoutnoexceptworks. Addingnoexceptto either fails to compile. It's as if MSVC treatsnoexceptas part of the type.py::class_<...>.def(...)is unaffected by thenoexceptspecifier.ikicic commentedon Nov 17, 2020
Could it be related to C++17? Compilation fails for me when using C++17 (g++ 7.3), but works when I either remove
noexceptor switch to C++14.If I understand correctly, C++17 introduced pointers to
noexceptfunctions, which are not covered by pybind's type traits (e.g. seeremove_classhere)dkavolis commentedon Nov 17, 2020
That must be it, it compiles with C++14 in MSVC as well.
[-][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[/+]bstaletic commentedon Nov 17, 2020
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
georgthegreat commentedon May 17, 2023
I can confirm the issue.
At the time python bindings for NVIDIA/TensorRT can not be compiler in post-c++17 mode.
henryiii commentedon Nov 12, 2026
🤖 AI text below 🤖
Fixed:
py::init(&T::create)with anoexceptfactory compiles in C++17 mode on current master.