我的代码有一点问题。 我尝试创建一个带有可变参数的函数,但是当我编译它时,它失败了,我真的不明白为什么。 所以如果有人能帮助我......
这是我的功能:
查询集.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.filter(Perso::_master,Perso::_lvl,"gt",4);//第135行
注意: Perso::_master 和 Perso::_lvl 是一些 static const std::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/Linaro 4.6.4-1ubuntu1~12.04),但我尝试使用 gcc4.8,但出现错误。
最佳答案
可变参数包必须出现在函数签名的末尾,而不是中间。
为了更好地理解,请阅读以下内容: Variadic function template with pack expansion not in last parameter
关于c++ - 可变参数模板参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18851918/