“没有匹配的调用函数”带有可变参数模板
我有一个类,旨在动态加载 .dll
或 .so
或等效文件。从那里,它将返回指向您要查找的任何函数的指针。不幸的是,我在实施过程中遇到了两个问题。
- 如果我使用返回 void* 作为函数指针的“哑”函数,当我尝试将它们操纵为我可以使用的表格。
- 如果我尝试使用带有可变参数模板和类型安全的“智能”函数,我将无法编译它。
错误:没有调用“Library::findFunction(std::string&)”的匹配函数
是这里唯一等待我的事情。正如您从下面的代码中看到的,这应该与函数签名匹配。一旦编译完成,问题 1 也会出现在这里。
作为参考,我在 Ubuntu 10.10 x86_64
下使用 g++ (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5
进行编译。我还尝试使用 g++-4.5 (Ubuntu/Linaro 4.5.1-7ubuntu2) 4.5.1 进行编译,但这不会改变任何内容。
#include <string>
#include <stdio.h>
class Library
{
public:
Library(const std::string& path) {}
~Library() {}
void* findFunction(const std::string& funcName) const
{
// dlsym will return a void* as pointer-to-function here.
return 0;
}
template<typename RetType, typename... T>
RetType (*findFunction(const std::string& funcName))(T... Ts) const
{
return (RetType (*)(...))findFunction(funcName);
}
};
int main()
{
Library test("/usr/lib/libsqlite3.so");
std::string name = "sqlite3_libversion";
const char* (*whatwhat)() = test.findFunction<const char*, void>(name);
// this SHOULD work. it's the right type now! >=[
//const char* ver3 = whatwhat();
//printf("blah says \"%s\"\n", ver3);
}
I have a class that is designed to dynamically load a .dll
or .so
or equivalent. From there, it will return pointers to whatever function you're trying to find. Unfortunately, I've come across two issues in my implementation.
- If I use the 'dumb' function returning void* as pointers to functions, I get
warning: ISO C++ forbids casting between pointer-to-function and pointer-to-object
when I try to manipulate them into a form I can use. - If I try using the 'smart' function with variadic templates and type safety, I can't get it to compile.
error: no matching function for call to ‘Library::findFunction(std::string&)’
is the only thing that awaits me here. As you can see from the code below, this should match the function signature. Once it does compile, issue 1 will be present here too.
For reference, I am compiling under Ubuntu 10.10 x86_64
with g++ (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5
. I have also tried compiling with g++-4.5 (Ubuntu/Linaro 4.5.1-7ubuntu2) 4.5.1
however this does not change anything.
#include <string>
#include <stdio.h>
class Library
{
public:
Library(const std::string& path) {}
~Library() {}
void* findFunction(const std::string& funcName) const
{
// dlsym will return a void* as pointer-to-function here.
return 0;
}
template<typename RetType, typename... T>
RetType (*findFunction(const std::string& funcName))(T... Ts) const
{
return (RetType (*)(...))findFunction(funcName);
}
};
int main()
{
Library test("/usr/lib/libsqlite3.so");
std::string name = "sqlite3_libversion";
const char* (*whatwhat)() = test.findFunction<const char*, void>(name);
// this SHOULD work. it's the right type now! >=[
//const char* ver3 = whatwhat();
//printf("blah says \"%s\"\n", ver3);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您需要将返回的函数签名更改为
T...
而不仅仅是...
否则它需要一个变量 arglist,即使它应该是空的。然后在类型列表中不使用
void
来调用它,它应该可以工作:通过这些更改,它可以在
gcc-4.5.1
上为我编译: http://ideone.com/UFbutI think you need to change the returned function signature to
T...
instead of just...
otherwise it expects a variable arglist even when it should just be empty.Then call it without the
void
in the type list and it should work:With these changes it compiles for me on
gcc-4.5.1
: http://ideone.com/UFbut