pybind / pybind/pybind11

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

Open
#2,234 9 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement help wanted
Dominant language
C++
Stars
18k
Forks
2.3k
Avg merge
5d 17h
Merged PRs (30d)
10

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by compiling the reproducible embedded-module example in C++17 and compare its behavior with C++11 or C++14. Investigate pybind11's overload system around inherited noexcept methods; the work is done when Derived::size() can be called successfully and returns 0 in the shown example.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python
Domain
api
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.