【发布时间】:2014-04-02 14:05:03
【问题描述】:
我有以下两个功能:
class Leaf {...};
void SpitLeaves(std::string & sdata, std::vector<Leaf> const & leaves);
void SpitLeaves(std::string & sdata, std::set<Leaf> const & leaves);
这些函数的定义是相同的。
这是模板化函数的明显候选者。
但是,尽管进行了搜索,但我无法弄清楚如何正确声明模板函数。从https://stackoverflow.com/a/4697356/368896 和其他人,我尝试过签名,例如:
template <template<typename> class T>
void SpitLeaves(std::string & sdata, T<Leaf> const & leaves)
{...}
但是,这会在我尝试实例化模板函数时出现编译器错误:
std::string leaves_str;
std::vector<Leaf> leaves;
SpitLeaves<std::vector>(leaves_str, leaves);
...错误(VS 2013)是'SpitLeaves' : template parameter list for class template 'std::vector' does not match template parameter list for template template parameter 'T'。
如何正确声明上述模板函数?
【问题讨论】: