std::thread 目标获取指针参数时出错

std::thread error for target taking pointer args

我有一个类似于下面的设置,

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;
}

我用

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

并得到:

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...>)

我无法解释这个错误。非常感谢任何帮助。

编辑:很抱歉,我遗漏了 foo 也有一个默认的 bool 参数设置为 false 的事实。去掉默认参数让它编译。

A.bar() 不是有效的语言结构。您只能在 A 的实例上调用 bar(),而不能在类型名称 A.

上调用

通过改变

A.bar();          

A a;
a.bar();

并提供 A::foo() 的虚拟实现,我能够构建并 运行 该程序。