简体   繁体   English

可变参数模板的“呼叫无匹配功能”

[英]“No matching function for call” with variadic templates

I have a class that is designed to dynamically load a .dll or .so or equivalent. 我有一个旨在动态加载.dll.so或等效文件的类。 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. 如果我使用返回空*的'dumb'函数作为函数的指针,则会收到warning: ISO C++ forbids casting between pointer-to-function and pointer-to-object尝试将它们操纵为可以使用的形式时在warning: ISO C++ forbids casting between pointer-to-function and pointer-to-object
  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. error: no matching function for call to 'Library::findFunction(std::string&)'是我唯一等待的地方。 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. 一旦编译完成,这里也会出现问题1。

For reference, I am compiling under Ubuntu 10.10 x86_64 with g++ (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5 . 作为参考,我正在使用g++ (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5Ubuntu 10.10 x86_64下进行编译。 I have also tried compiling with g++-4.5 (Ubuntu/Linaro 4.5.1-7ubuntu2) 4.5.1 however this does not change anything. 我也尝试过使用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 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. 我认为您需要将返回的函数签名更改为T...而不是...否则,即使它应该为空,它也需要一个变量arglist。

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: 然后在类型列表中不带void情况下调用它,它应该可以工作:

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

With these changes it compiles for me on gcc-4.5.1 : http://ideone.com/UFbut 有了这些更改,它就可以在gcc-4.5.1上为我编译: http : //ideone.com/UFbut

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM