In the Cocoa Framework, all NSObject
s implement a method - respondsToSelector:
that returns a Boolean value indicating whether the receiver implements or inherits a method that can respond to a specified message. So if I wanted to check if my object foo
has a method - bar:
, I would use the message [foo respondsToSelector: @selector(bar:)];
.
Is this behavior possible in C++ with an identical interface, either by implementing selectors as a struct
or through using std::function
objects as parameters? For example, the function definition would look like one of the following:
bool Foo::respondsToSelector(const std::function<?> &selector) {
// Problem: Template arg and return types of this call would always be unknown.
}
bool Foo::respondsToSelector(const Selector *selector) {
}
Objective-C can determine selectors at either compile-time or run-time depending on whether @selector()
or NSSelectorFromString()
is used. I am asking for either.