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
noexcept base class methods are not recognized in C++17 mode #2234
Base class methods attributed with noexcept are not recognized by pybind11 overload system in C++17 mode. The example below compiles just fine in C++11/C++14 mode. Probably it's related to:
The noexcept-specification is a part of the function type and may appear as part of any function declarator. (since C++17)
terminate called after throwing an instance of 'pybind11::error_already_set'
what(): TypeError: size(): incompatible function arguments. The following argument types are supported:
1. (self: Base) -> int
Invoked with: <example.Derived object at 0x7f42fb179ce0>
At:
<string>(5): <module>
Python/Pybind11 doesn't know that Base is a base class of Derived. You can make this work like so:
#include "pybind11/pybind11.h"
#include "pybind11/embed.h"
namespace py = pybind11;
struct Base {
int size() const noexcept { return 0; }
};
struct Derived : Base {};
PYBIND11_EMBEDDED_MODULE(example, m) {
py::class_<Base>(m, ""); // Bind the base class
py::class_<Derived, Base>(m, "Derived") // Tell python about the inheritance hierarchy
.def(py::init<>())
.def("size", &Derived::size);
}
int main() {
py::scoped_interpreter guard{};
py::exec(R"(
from example import Derived
d = Derived()
print(d.size())
)");
}
Sorry for unclear description. In my example I don't want to expose Base class to python. The snippet works fine without noexcept specifier, so I still think it's a bug.
We recently added a special case for ref-qualified functions (63df87f), now this seems like yet another annotation that would need to be supported. Not super-happy about the potential combinatorial explosion here, it would be nice if C++ would let us template over such things..
Okay, there is definitely a way around templating this, but I am bit lost on how to refactor this to proceed. We just need to use std::mem_fn and std::invoke (backported) to acomplish this. The C+11 backport could look something like this:
I'm running into this problem. I have a base class, which uses noexcept and which I cannot change, that pybind11 fails to find. Is there a workaround that I can use in the derived class to get around this?
Activity
bstaletic commentedon Jul 7, 2020
Python/Pybind11 doesn't know that
Baseis a base class ofDerived. You can make this work like so:sizmailov commentedon Jul 7, 2020
Sorry for unclear description. In my example I don't want to expose
Baseclass to python. The snippet works fine withoutnoexceptspecifier, so I still think it's a bug.wjakob commentedon Jul 7, 2020
We recently added a special case for ref-qualified functions (63df87f), now this seems like yet another annotation that would need to be supported. Not super-happy about the potential combinatorial explosion here, it would be nice if C++ would let us template over such things..
bstaletic commentedon Jul 7, 2020
Did someone say Reflections? ...
Skylion007 commentedon Oct 31, 2022
I think it might actually be possible to template over such things (at least in C++17 which is the only place it is necessary) by abusing/reimplementing std::invoke: https://stackoverflow.com/questions/60852108/overload-regardless-noexcept-specification (C++11 backport available here): https://web.archive.org/web/20150811205403/https://github.com/tomaszkam/proposals/blob/master/invoke/invoke_cpp11.hpp Stumped on how to actually implement this though, we may need to use std::invoke_result
Skylion007 commentedon Nov 13, 2022
Okay, there is definitely a way around templating this, but I am bit lost on how to refactor this to proceed. We just need to use std::mem_fn and std::invoke (backported) to acomplish this. The C+11 backport could look something like this:
Skylion007 commentedon Nov 16, 2022
This template is horrific, but may be the easiest way to use downstream as it just converts noexcept functions types to none-noexcept function types. https://stackoverflow.com/a/55701361/2444240 Would still need to refactor the initialize overloads to use std::mem_fn though which seems difficult.
Skylion007 commentedon Mar 28, 2023
Places that need updating are listed in this comment: #2856 (comment)
mayaknife commentedon Apr 29, 2024
I'm running into this problem. I have a base class, which uses noexcept and which I cannot change, that pybind11 fails to find. Is there a workaround that I can use in the derived class to get around this?
Close remaining qualifier consistency gaps for member pointer bindings.
fix: bind noexcept and ref-qualified methods from unregistered base c…
fix: bind noexcept and ref-qualified methods from unregistered base c…
henryiii commentedon Nov 12, 2026
🤖 AI text below 🤖
Fixed by #5992, which binds
noexceptand ref-qualified methods from base classes that are not registered.