I cannot understand why GCC generates an error on the following C++ code:
template<class T>
void f(T &x, void(T::*g)())
{
x.g();
}
struct A
{
void h(){;}
};
int
main(int, char*[])
{
A a;
f(a, &A::h);
return 0;
}
This is what GCC generates as output when trying to compile:
g++ -std=c++1y -O0 -g3 -pedantic -Wall -Wextra -Wconversion -Weffc++ -c -fmessage-length=0 --std=c++2a -Weffc++ -o Main.o ../Main.cpp
../Main.cpp: In instantiation of ‘void f(T&, void (T::*)()) [with T = A]’:
../Main.cpp:16:13: required from here
../Main.cpp:4:5: error: ‘struct A’ has no member named ‘g’
4 | x.g();
| ~~^
../Main.cpp:2:14: warning: unused parameter ‘g’ [-Wunused-parameter]
2 | void f(T &x, void(T::*g)())
|
GCC seems to look for a member 'g' of class A even though the method parameter has been instantiated with A's member function h.