0

Here's the code:

#include <iostream>
#include <thread>

struct PickRandomFile {
    PickRandomFile() {
        std::thread t1(taskScanPaths);
    }

    inline void taskScanPaths() {
        // my task
    }
};

int main() {
    PickRandomFile pickRandomFile;

    return 0;
}

msvc says PickRandomFile::taskScanPaths': non-standard syntax; use '&' to create a pointer to member ThreadTrigger

What's wrong? I usually do it in gcc.

markzzz
  • 47,390
  • 120
  • 299
  • 507
  • 3
    You're looking for `std::thread t1(&PickRandomFile::taskScanPaths, this);`. – Quentin Oct 22 '19 at 13:21
  • 1
    https://stackoverflow.com/questions/10673585/start-thread-with-member-function – Cory Kramer Oct 22 '19 at 13:22
  • That's not required in gcc. Why? – markzzz Oct 22 '19 at 13:23
  • 2
    Which gcc version does compile this code? All I've tried give me an error `error: invalid use of non-static member function 'void PickRandomFile::taskScanPaths()'` (bit less descriptive in earlier versions). [online compiler](https://wandbox.org/permlink/zvW0ssYTe8LH9GZu) – Yksisarvinen Oct 22 '19 at 13:23
  • @Yksisarvinen: `gcc.exe (Rev3, Built by MSYS2 project) 9.1.0` – markzzz Oct 22 '19 at 13:24
  • fails for all for me: https://godbolt.org/z/zocZja – NathanOliver Oct 22 '19 at 13:25
  • Not sure, it works fine with that version on other projects :P – markzzz Oct 22 '19 at 13:27
  • 3
    @markzzz In those other projects, are you using class functions or free functions? – NathanOliver Oct 22 '19 at 13:31
  • 1
    @markzzz As the message says: It's **non-standard syntax**, i.e. something that msvc/gcc can graciously allow that would be invalid according to the C++ standard. The reasons/conditions when it allows this (or even prints a warning/errors out) are probably dependent on your compiler version and build flags (assuming that this very same code works in other circumstances). – Max Langhof Oct 22 '19 at 13:48
  • @NathanOliver as the code above: a "inline void" function within a class (a struct=. Would you call it free? – markzzz Oct 22 '19 at 14:08
  • @markzzz No. A free function is a function at global or namespace scope. Making it inline doesn't do anything here since member functions that are defined inside a class are implicitly inline. – NathanOliver Oct 22 '19 at 14:09
  • @NathanOliver I know :) Still it doesn't prompt nothing. I can live with it anyway, thanks – markzzz Oct 22 '19 at 14:25

1 Answers1

3

Free functions "decay to pointers" (similar to arrays) so

std::thread t1(taskScanPaths);

would have been ok if taskScanPaths was a free function and it would have same effect as

std::thread t1(&taskScanPaths);

However, for class member function you need the address-of to get a pointer to the member function (and you need to specify the class), as in

std::thread t1(&PickRandomFile::taskScanPaths,this);

Also note that you need to pass a object/pointer to an instance so the thread can actually call the method.

Some relevant quotes from cppreference:

Pointer to member functions

A pointer to non-static member function f which is a member of class C can be initialized with the expression &C::f exactly. Expressions such as &(C::f) or &f inside C's member function do not form pointers to member functions.

It does not explicitly mention f, but as &f does not form a pointer to member function, it is kinda safe to assume f also does not form a pointer to member function.

On the other hand:

Pointers to functions

A pointer to function can be initialized with an address of a non-member function or a static member function. Because of the function-to-pointer implicit conversion, the address-of operator is optional:

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185