[英]Assign generic function to function pointer struct memeber
我必须分配结构的以下成员:
esp_err_t (*handler)(httpd_req_t *r);
如您所见,它是一个 function 指针。 我有一个通用模板 function 我想指定为handler
:
template <class Tmsg>
esp_err_t HandleRpc(httpd_req_t *req){...}
我在通用模板 class 中分配handler
成员,所以我有一个通用类型参数Tpayload
:
httpd_uri_t cfg = {
...
.handler = HandleRpc<Tpayload>,
...
};
我得到:
'>' 标记之前的预期主表达式
问题在于我无法传递成员方法指针(IE esp_err_t (RpcServer::*)(...)
),但RpcServer
是通用模板 class (IE 有一个带有一个通用参数的模板)。 So I thought that by creating a generic template function outisde the class (global scope?), and passing the RpcServer
instance into that function, I would be able to retrieve my instance of RpcServer<T>
and all would be well.
这是我能想出的重现该问题的最少代码量:
int main()
{
}
template <class T>
class RpcServer{
public:
void RegisterHandler();
};
struct HandlerInfo{
void (*handler)();
};
template <class T>
void Handle(RpcServer<T> test)
{
}
template <class T>
void RpcServer<T>::RegisterHandler(){
HandlerInfo info = {
.handler = Handle<T>;
};
}
我错过了显而易见的事情,还是我想做的事情需要一些更丑陋的诡计?
struct HandlerInfo{
void (*handler)();
};
handler
是一个指向 function 的指针,它不带参数,也不返回任何东西。 您可以将此指针设置为指向任何 function。 只要它不带参数,并且不返回任何东西(它的返回类型是void
)。 这一点也不例外,这就是 C++ 的工作原理,它是一种强类型语言。
template <class T>
void Handle(RpcServer<T> test)
{
这是采用一个参数的 function 的模板。 参数的类型不重要。 关键是这个模板的每个实例都是一个 function,它总是只接受一个参数。
在C++中,指向没有参数的function的指针只能设置为指向这样的function。 您不能将此 function 指针设置为指向一个 function 接受一个参数、两个参数或十个参数。 它只能设置为采用零参数的 function。 那是因为那是指针所指向的。
如果您要更改模板 function 使其不带参数,那么这当然可以:
int main()
{
}
template <class T>
class RpcServer{
public:
void RegisterHandler();
};
struct HandlerInfo{
void (*handler)();
};
template <class T>
void Handle()
{
}
template <class T>
void RpcServer<T>::RegisterHandler(){
HandlerInfo info = {
.handler = Handle<T>
};
}
这在 gcc 10 上编译。“.member”初始化语法已被 gcc 支持很长时间,但它只是在 C++20 时才标准化,因此其他编译器可能不支持此语法。
如果您愿意,可以将其声明为指向以RpcServer<int>
作为参数的 function 的指针:
struct HandlerInfo{
void (*handler)(RpcServer<int>);
};
现在,您将能够将其初始化为指向这样的 function:
HandlerInfo info = {
.handler = Handle<int>
};
HandleInt
实例化了一个采用此类参数的 function,因此类型完全匹配。
或者,或者,使HandlerInfo
本身成为匹配模板:
template <class T>
class RpcServer{
public:
void RegisterHandler();
};
template<class T>
struct HandlerInfo{
void (*handler)(RpcServer<T>);
};
template <class T>
void Handle(RpcServer<T> )
{
}
template <class T>
void RpcServer<T>::RegisterHandler(){
HandlerInfo<T> info = {
.handler = Handle<T>
};
}
int main()
{
RpcServer<int> server;
server.RegisterHandler();
}
(注意——你的代码有其他语法错误;如果它们被修复,看起来,一开始,代码会编译;但如果尝试实例化模板,它会由于类型不匹配而失败)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.