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.

12
  • 3
    You're looking for std::thread t1(&PickRandomFile::taskScanPaths, this);. Commented Oct 22, 2019 at 13:21
  • 1
    stackoverflow.com/questions/10673585/… Commented Oct 22, 2019 at 13:22
  • 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 Commented Oct 22, 2019 at 13:23
  • 3
    @markzzz In those other projects, are you using class functions or free functions? Commented Oct 22, 2019 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). Commented Oct 22, 2019 at 13:48

1 Answer 1

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:

Sign up to request clarification or add additional context in comments.

4 Comments

don't forget you need a object/pointer of the class type as the second parameter if it is a non-static function.
@NathanOliver thanks fixed. Anyhow I didnt pay attention to who is the asker and it seems that focus is more on "why gcc allowed it", I guess its comes as non-standard extension
AFAIK it can't ever work. GCC can't know which object to call the function on. My guess is the OP had free functions in the other examples.
@NathanOliver reading a bit I also could convince myself of the same, will add a bit more

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.