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