“没有匹配的调用函数”带有可变参数模板

发布于 2024-12-27 08:22:17 字数 1412 浏览 2 评论 0原文

我有一个类,旨在动态加载 .dll.so 或等效文件。从那里,它将返回指向您要查找的任何函数的指针。不幸的是,我在实施过程中遇到了两个问题。

  1. 如果我使用返回 void* 作为函数指针的“哑”函数,当我尝试将它们操纵为我可以使用的表格。
  2. 如果我尝试使用带有可变参数模板和类型安全的“智能”函数,我将无法编译它。 错误:没有调用“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.

  1. 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.
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等待圉鍢 2025-01-03 08:22:17

我认为您需要将返回的函数签名更改为 T... 而不仅仅是 ... 否则它需要一个变量 arglist,即使它应该是空的。

template<typename RetType, typename... T>
RetType (*findFunction(const std::string& funcName))(T... Ts) const
{
    return (RetType (*)(T...))findFunction(funcName);
}

然后在类型列表中不使用 void 来调用它,它应该可以工作:

const char* (*whatwhat)() = test.findFunction<const char*>(name);

通过这些更改,它可以在 gcc-4.5.1 上为我编译: http://ideone.com/UFbut

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.

template<typename RetType, typename... T>
RetType (*findFunction(const std::string& funcName))(T... Ts) const
{
    return (RetType (*)(T...))findFunction(funcName);
}

Then call it without the void in the type list and it should work:

const char* (*whatwhat)() = test.findFunction<const char*>(name);

With these changes it compiles for me on gcc-4.5.1: http://ideone.com/UFbut

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文