Is it possible to detect whether a class has a member function with a specific name? Note, that the parameter list of the function doesn’t matter, and the function can even be a template function, or the function name can be overloaded.
Detecting whether a member function exists with a known parameter list seems to be easy by using requires
:
#include <cstdio>
struct Foo {
void something(int);
};
template <typename TYPE>
concept has_something = requires(TYPE t) { t.something(42); };
int main() {
printf("has_something: %dn", has_something<Foo>);
}
But I’d like to detect whether a function exists with any parameter list. If I remove the int
parameter from something
, has_something
will be false
.
(I tried to use member pointers in requires
, like &TYPE::something;
, but it doesn’t work if something
is overloaded, as the expression is ambiguous. Not to mention that it doesn’t work with template functions either.)
14