pybind/pybind11

noexcept base class methods are not recognized in C++17 mode

Open

#2234 opened on May 29, 2020

View on GitHub
 (9 comments) (0 reactions) (0 assignees)C++ (14,677 stars) (2,005 forks)batch import
enhancementhelp wanted

Description

Issue description

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)

https://en.cppreference.com/w/cpp/language/noexcept_spec#Explanation

Reproducible example code

#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_<Derived>(m, "Derived")
      .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())
)");
}

Output (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>

(tried with g++-8 / clang-9)

Contributor guide