Are pointers to pure virtual member functions of local classes allowed?
06:22 31 Oct 2025

Consider the following snippet:

int main() {
  struct Local {
    virtual void foo() = 0;
  };

  void (Local::* ptr)() = &Local::foo;
}

When compiling with C++20, GCC 13.3.0 and Clang 18.1.3 both compile this code, but MSVC 19.39 thru 19.43 generate the following compiler error:

(3): error C3640: 'main::Local::[thunk]: __cdecl `int __cdecl main(void)'::`2'::Local::`vcall'{0,{flat}}' }'': a referenced or virtual member function of a local class must be defined

I could not find anything in the C++20 standard to prove either compiler right. Is the code snippet well-formed?

Example on Compiler Explorer

c++ visual-c++ language-lawyer pure-virtual local-class