c++functionstdthread

pointer-to-member syntax difference in g++ 13 (Linux) vs. g++ 14 (Windows)


I experienced an interesting behavior of g++ in versions 13 and 14. The following code works fine in g++14 (MSYS2, MinGW, Windows):

#include <iostream>
#include <thread>

class A
{
public:
    void f()
    {
        std::cout<<"Okay!\n";
    }
};

int main(int argc,char **argv)
{
    A a;
    std::thread t(A::f,&a);
    t.join();
    return 0;
}

Trying to compile with g++13 (Linux), however, yields:

error: invalid use of non-static member function 'void A::f()'

Changing just

std::thread t(A::f,&a);

to

std::thread t(&A::f,&a);

solves the issue. Seemingly, under the non-MSYS2 variant a member function could not be used in place of a function argument, but a pointer to a member function could?

This was tested under g++14 under MSYS2 (Windows 10). Apparently, other g++14 versions do not work.

$ g++ --version
g++.exe (Rev2, Built by MSYS2 project) 14.2.0

Solution

  • This is invalid C++. This works for you because MinGW GCC implicitly enables -fms-extensions (imitating MSVC's non-standard features).

    I doubt this is new in GCC 14. I don't have a MinGW GCC 13 at hand to test, but it always did this (added the flag), and GCC 13 on Linux with this flag accepts your code too.

    Pass -fno-ms-extensions to disable this nonsense. I'm not sure why it's on by default. (And you'd think -std=c++?? -pedantic-errors would disable this, but alas it doesn't.)