I wrote the following program that is accepted by msvc but both gcc and clang rejects it.
struct C
{
void Bar(int);
};
int main()
{
void (C::*ptr)(int) = &(C::Bar); //compiles with msvc but both gcc and clang rejects this
}
Gcc says:
<source>: In function 'int main()':
<source>:7:32: error: invalid use of non-static member function 'void C::Bar(int)'
7 | void (C::*ptr)(int) = &(C::Bar); //compiles with msvc but both gcc and clang rejects this
| ^~~
Compiler returned: 1
Which compiler is correct?
This is a msvc bug. The standard explicitly forbids this in expr.unary.op:
(emphasis mine)
This means that the program is ill-formed and msvc is wrong in accepting the code.
Here is the msvc bug:
MSVC accepts invalid program involving member function pointer