[英]“No matching function for call” with variadic templates
我有一個旨在動態加載.dll
或.so
或等效文件的類。 從那里,它將返回指向您要查找的任何函數的指針。 不幸的是,我在實現過程中遇到了兩個問題。
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
。 error: no matching function for call to 'Library::findFunction(std::string&)'
是我唯一等待的地方。 從下面的代碼中可以看到,這應該與函數簽名匹配。 一旦編譯完成,這里也會出現問題1。 作為參考,我正在使用g++ (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5
在Ubuntu 10.10 x86_64
下進行編譯。 我也嘗試過使用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);
}
我認為您需要將返回的函數簽名更改為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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.