Search code examples
c++c++14stdthread

std::thread error for target taking pointer args


I have a setup similar to the following,

A.hpp

#include <thread>

class A {

    static void foo(char*, char*);

    void bar() {

        char* char_start = (char*) malloc(100 * sizeof(char));
        char* char_end = char_start + (100 * sizeof(char)) - 1;

        std::thread t(foo, char_start, char_end);
        t.join();

        return;
    }
};

main.cpp

int main() { 
    A.bar();            

    return 0;
}

I compile with

g++ -std=c++14 -pthread main.cpp

And get:

usr/include/c++/4.9/functional: In instantiation of ‘struct std::_Bind_simple<void (*(long unsigned int*, char*, char*))(long unsigned int*, char*, char*, const bool&)>’:
/usr/include/c++/4.9/thread:140:47:   required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (&)(long unsigned int*, char*, char*, const bool&); _Args = {long unsigned int*, char*, char*}]’
A.cpp:100:151:   required from here
/usr/include/c++/4.9/functional:1665:61: error: no type named ‘type’ in ‘class std::result_of<void (*(long unsigned int*, char*, char*))(long unsigned int*, char*, char*, const bool&)>’
typedef typename result_of<_Callable(_Args...)>::type result_type;
                                                         ^
/usr/include/c++/4.9/functional:1695:9: error: no type named ‘type’ in ‘class std::result_of<void (*(long unsigned int*, char*, char*))(long unsigned int*, char*, char*, const bool&)>’
 _M_invoke(_Index_tuple<_Indices...>)

I can't interpret this error. Any help is much appreciated.

EDIT: I apologize as I left out the fact that foo also had a default bool argument set to false. Getting rid of the default argument let's it compile.


Solution

  • A.bar() is not a valid language construct. You can invoke bar() only on an instance of A, not on the typename A.

    By changing

    A.bar();          
    

    to

    A a;
    a.bar();
    

    and providing a dummy implementation of A::foo(), I was able to build and run the program.