我的代码有个小问题。我试图创建一个带有变量参数的函数,但是当我编译它时,它失败了,我真的不明白为什么。所以如果有人能帮我..。
这是我的功能:
QuerySet.hpp:
template <typename T>
class QuerySet
{
template<typename U,typename ... Args>
QuerySet& filter(const std::string& colum,Args ... args,const std::string& operation, const U& value);
//...
}
template<typename T>
template<typename U,typename ... Args>
QuerySet<T>& QuerySet<T>::filter(const std::string& colum,Args ... args,const std::string& operation, const U& value)
{
//some job
return *this;
}main.cpp QuerySet queryset;queryset.filter(Perso::_master,Perso::_lvl,"gt",4);//第135行
注: Perso::_master和Perso::_lvl是一些静态的const::string;
错误:
g++ -g -std=c++0x -I"/my_path/cpp-ORM" -lmysqlcppconn -o main.o -c main.cpp;
main.cpp: In function ‘int main(int, char**)’:
main.cpp:135:46: erreur: no matching function for call to ‘orm::QuerySet<Perso>::filter(const string&, const string&, const string&, int)’
main.cpp:135:46: note: candidate is:
/my_path/QuerySet.hpp:18:23: note: template<class U, class ... Args> orm::QuerySet<T>& orm::QuerySet::filter(const string&, Args ..., const string&, const U&) [with U = U, Args = {Args ...}, T = Perso, std::string = std::basic_string<char>]信息:我使用gcc版本4.6.4 (Ubuntu/Linaro4.6.4-1ubuntu1~12.04),但我尝试使用gcc4.8,而且我有一个错误。
发布于 2013-09-17 14:11:42
您的函数永远不会被调用,这是一个模板参数无法推断的上下文:
n3337,14.8.2.1/1 temp.deduct.call
模板参数推断是通过比较每个函数模板参数类型(称为P)和调用的相应参数类型(称为A)来完成的,如下所述。 ..。 对于在参数声明列表末尾出现的函数参数包,调用的每个剩余参数的类型A与函数参数包的声明符-id的类型P进行比较。每个比较都会为由函数参数包展开的模板参数包中的后续位置推导模板参数。对于没有出现在参数声明列表末尾的函数参数包,参数包的类型是一个非推导的上下文.。 ..。
将参数包移到函数参数列表的末尾。
您可以不用显式地指定模板参数,但我认为这不是您想要的。例如:
q.filter<int, int, int>("oi", 1, 2, "oi", i);发布于 2013-09-17 14:06:25
变量参数包必须发生在函数签名的末尾,而不是中间。
要更好地理解,请阅读以下文章:Variadic function template with pack expansion not in last parameter
https://stackoverflow.com/questions/18851918
复制相似问题