I understand how to declare the type of a function:
typedef void (typedef_void_f)(); // typedef_void_f is void()
using alias_void_f = void(); // alias_void_f is void()
And it can be used to declare function pointers:
void function() { std::cout << __PRETTY_FUNCTION__ << '\n'; }
typedef_void_f *a = function; // pointer to void()
alias_void_f *b = function; // pointer to void()
For member function pointers the syntax is slightly more complicated:
struct S { void function() { std::cout << __PRETTY_FUNCTION__ << '\n'; } };
typedef void (S::*typedef_void_m_f)();
using alias_void_m_f = void (S::*)();
typedef_void_m_f c = &S::function; // pointer to S void() member function
alias_void_m_f d = &S::function; // pointer to S void() member function
This is my understanding of function pointers in C++ and I thought that it was enough.
But in the p0172r0 technical paper I've found a syntax that I'm not familiar:
struct host {
int function() const;
};
template <typename TYPE>
constexpr bool test(TYPE host::*) { // <---- What is this??
return is_same_v<TYPE, int() const>;
}
constexpr auto member = &host::function;
test(member);
As I understand the code, the test
function splits the type of the function from the type of the object where the function belongs, so in the template test
function the TYPE
template parameter would be void()
but if I try the following:
void my_test(void() S::*) {}
my_test(&S::function);
I get a bunch of syntax errors:
error: variable or field 'my_test' declared void void my_test(void() S::*) {} ^ error: expected ')' before 'S' void my_test(void() S::*) {} ^ error: 'my_test' was not declared in this scope my_test(&S::function);
So is obvious that I'm not understanding the p0172r0's test
function syntax.
Can someone explain the details of the template <typename TYPE> constexpr bool test(TYPE host::*)
syntax?