cppyy
cppyy copied to clipboard
Exception missing for compile error triggered by call to C++ function
In the following test program I make a call to a non-template member function of a class template. Template instantiation of this function causes a compile error. The error is correctly printed to stdout, but cppyy fails to raise any exception.
repro.py:
import cppyy
cppyy.include("string")
cppyy.include("type_traits")
cppyy.cppdef("""
template <typename Allowed> struct handler {
template <typename Event,
typename = std::enable_if_t<std::is_same_v<Event, Allowed>>>
void handle(Event const &event) {}
};
template <typename Handler> struct wrapper {
Handler handler;
explicit wrapper(Handler handler) : handler(handler) {}
void handle(std::string const &event) {
handler.handle(event); // Error with Handler = handler<int>
}
};
auto make_wrapped() {
return wrapper(handler<int>());
}
""")
w = cppyy.gbl.make_wrapped() # OK, handle() template not instantiated yet
print("Calling handle(std::string const &) on wrapper")
# Expected: SyntaxError (or some exception)
# Actual: No exception; yet prints the compile error to stderr
r = w.handle("abc")
print("Did not raise exception")
$ python repro.py
Calling handle(std::string const &) on wrapper
input_line_20:14:17: error: no matching member function for call to 'handle'
handler.handle(event); // Error with Handler = handler<int>
~~~~~~~~^~~~~~
input_line_22:6:35: note: in instantiation of member function 'wrapper<handler<int>>::handle' requested here
((wrapper<handler<int>>*)obj)->handle((const std::string&)*(const std::string*)args[0]);
^
input_line_20:5:10: note: candidate template ignored: requirement 'std::is_same_v<std::string, int>' was not satisfied [with Event = std::string]
void handle(Event const &event) {}
^
Did not raise exception
cppyy 3.1.2, macOS arm64 or x86-64.
(The fact that there is no immediate error from the cppdef()
part, and that the call to wrapper<...>::handle()
is what triggers the compile error, matches the behavior of Clang 13 (Compiler Explorer) as well as other compilers. The only problem here is that there is no way for the Python code to be aware of the error.)