Question

MSVC rejects program with member function call while gcc and clang accept

I wrote the following program in C++23 that compiles with gcc and clang but is rejected by msvc. I want to know is this well-formed or ill-formed etc as per the standard. Live demo

struct C
{
    void f(this C);  
    
    void b()
    {
        (C::f)(); //compiles in gcc and clang but msvc rejects 
    }
};

int main()
{ 
      C c;
      c.b();       
}

MSVC says:

<source>(7): error C2352: 'C::f': a call of a non-static member function requires an object
<source>(3): note: see declaration of 'C::f'
 8  316  8
1 Jan 1970

Solution

 7

is this well-formed or ill-formed etc as per the standard

The program is well-formed as explained below and currently msvc only partially support P0847R7.

This can be seen from expr.prim.id:

If an id-expression E denotes a non-static non-type member of some class C at a point where the current class ([expr.prim.this]) is X and

  • E is potentially evaluated or C is X or a base class of X, and E is not the id-expression of a class member access expression ([expr.ref]), and
  • if E is a qualified-id, E is not the un-parenthesized operand of the unary & operator ([expr.unary.op]),

the id-expression is transformed into a class member access expression using (*this) as the object expression.

This means that the (C::f)() is transformed into ((*this).f)() which is perfectly well-formed and is accepted by all three compilers.


Note that msvc currently only partially support explicit object parameter as can be seen from compiler support:

C++23 feature Paper(s) GCC Clang MSVC
Explicit object parameter (deducing this) P0847R7 14 18 19.32*(partial)*
2024-07-04
user12002570