C++ 指向函数模板实例的指针
我想使用thunk函数模板将其传递给pthread_create之类的东西 我希望编译器用给定的参数实例化函数,所有信息都在那里,然后使用typedef作为函数ptr传递给这些函数C++ 指向函数模板实例的指针,c++,linux,templates,gcc,C++,Linux,Templates,Gcc,我想使用thunk函数模板将其传递给pthread_create之类的东西 我希望编译器用给定的参数实例化函数,所有信息都在那里,然后使用typedef作为函数ptr传递给这些函数 #include <string> class ServerImpl{ public: ServerImpl(std::string host, int port); void run(); }; template<typename T,void (T:
#include <string>
class ServerImpl{
public:
ServerImpl(std::string host, int port);
void run();
};
template<typename T,void (T::*mem_fn)()>
void *thunk(void *obj) {
(static_cast<T*>(obj)->*mem_fn)();
return 0;
}
typedef void *(*Function) (void);
Function fun = (Function)&thunk<ServerImpl,&ServerImpl::run>;
fun();
#包括
类ServerImpl{
公众:
ServerImpl(std::string主机,int端口);
无效运行();
};
模板
void*thunk(void*obj){
(静态铸造(obj)->*mem_fn)();
返回0;
}
typedef void*(*函数)(void);
函数fun=(函数)&thunk;
乐趣();
刚刚更改了它
看,通过这个我得到了一个链接器错误,这就是它给我带来的上一个错误。忽略我应该传递一个对象的事实
Server.o: In function `ServerImpl::ServerImpl(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int)':
Server.cpp:(.text+0x11f): undefined reference to `void* thunk<ServerImpl, &(ServerImpl::run())>(void*)'
Server.o:在函数'ServerImpl::ServerImpl(std::basic_string,int)'中:
Server.cpp:(.text+0x11f):对“void*thunk(void*)”的未定义引用
thunk
是一个函数模板。如何在其上使用typedef
?这没有道理。它不是一种类型。只能对类型应用typedef
您应该这样做:
ServerImpl *arg = new ServerImpl(); //why do I use new?
pthread_create(&thread_id, NULL, &thunk<ServerImpl,&ServerImpl::run>, arg);
这是错误的。因为thunk
接受一个参数。所以你应该这样做:
Function fun = (Function)&thunk<ServerImpl,&ServerImpl::run>;
fun(new ServerImpl); //pass an argument
函数fun=(函数)&thunk;
乐趣(新服务器)//辩论
成员函数
run
根据您传递的参数调用。thunk
是一个函数模板。如何在其上使用typedef
?这没有道理。它不是一种类型。只能对类型应用typedef
您应该这样做:
ServerImpl *arg = new ServerImpl(); //why do I use new?
pthread_create(&thread_id, NULL, &thunk<ServerImpl,&ServerImpl::run>, arg);
这是错误的。因为thunk
接受一个参数。所以你应该这样做:
Function fun = (Function)&thunk<ServerImpl,&ServerImpl::run>;
fun(new ServerImpl); //pass an argument
函数fun=(函数)&thunk;
乐趣(新服务器)//辩论
成员函数
run
在您传递的这个参数上被调用。错误很明显。那就像试着去做一样
void f() { }
typedef f blah;
或
你只需要这样做
pthread_create(&thread_id, &thunk<ServerImpl, &ServerImpl::run>);
pthread\u创建(&thread\u id,&thunk);
或者,如果您愿意:
auto threadproc = &thunk<ServerImpl, &ServerImpl::run>;
pthread_create(&thread_id, threadproc);
auto-threadproc=&thunk;
pthread_create(&thread_id,threadproc);
而且,在风格方面,请在参数列表中的每个逗号后面使用空格:)错误很明显。那就像试着去做一样
void f() { }
typedef f blah;
或
你只需要这样做
pthread_create(&thread_id, &thunk<ServerImpl, &ServerImpl::run>);
pthread\u创建(&thread\u id,&thunk);
或者,如果您愿意:
auto threadproc = &thunk<ServerImpl, &ServerImpl::run>;
pthread_create(&thread_id, threadproc);
auto-threadproc=&thunk;
pthread_create(&thread_id,threadproc);
而且,在风格方面,请在参数列表中的每个逗号后面使用空格:)我不知道编译器是否正试图告诉您,但是
thunk
的签名没有使用任何模板类型,您希望编译器如何猜到它
这里pthread_create需要一个函数而不是类型(typedef fun define a type,not a function)我不知道编译器是否正试图告诉您这一点,但是
thunk
的签名没有使用任何模板类型,您希望编译器如何猜测它
这里pthread_create需要的是一个函数而不是一个类型(typedef fun定义一个类型,而不是一个函数)不,不是那样。函数名不是类型,因此无法为其创建typedef别名。不,不是那样。函数名不是类型,因此无法为其创建typedef别名。与问题无关,对于线程,请使用Boost.Thread或C++11标准库线程。与问题无关,对于线程,请使用Boost.Thread或C++11标准库线程。是的,这会编译,但随后我发现链接器错误与thunk:
Server.cpp:(.text+0x140):对
void*thunk(void*)的未定义引用“`是的,这可以编译,但是我发现thunk的链接器错误:Server.cpp:(.text+0x140):对
void*thunk(void*)的未定义引用”`嘿,pthread_create是一个例子,并不是真的这么做,但是看到我的更新,只是尝试在链接时调用函数失败。不,检查typedef,我将舍弃该参数以通过其他签名传递它,然后我将舍弃它以接收该参数。在任何不应停止编译或链接时间的情况下,编译器都无法找到thunk实例。@ArkaitzJimenez:这是错误的。你不能经常那样做。是乌布。@ArkaitzJimenez:看到这个了吗:。你应该这样做。请注意,为了简单起见,我将ServerImpl
的构造函数设为无参数。嘿,pthread\u create是一个示例,并没有真正这样做,但请参阅我的更新,只是尝试在链接时调用函数失败。否,请检查typedef,我将舍弃该参数以通过其他签名传递它,然后我将舍弃它以接收该参数。在任何不应停止编译或链接时间的情况下,编译器都无法找到thunk实例。@ArkaitzJimenez:这是错误的。你不能经常那样做。是乌布。@ArkaitzJimenez:看到这个了吗:。你应该这样做。请注意,为了简单起见,我将ServerImpl
的构造函数设置为无参数。