假设我有类似的类模板:
template<typename CONTAINER, typename param>
struct someClass {
static void foo (CONTAINER someContainer) {
//some code which needs to know both the container as well as its parameter
}
};
现在我想传递一个向量以及向量模板专用的类型:
int main () {
std::vector<int> someVector;
someClass<std::vector<int>, int>::foo(someVector);
}
有没有办法让这个更好,以便我可以使用:
someClass<std::vector<int>>::foo(someVector);
提前谢谢。
答案 0 :(得分:3)
如果我理解正确,你想:
template<typename CONTAINER, typename param = typename CONTAINER::value_type>
struct someClass {
static void foo (CONTAINER someContainer) {
//some code which needs to know both the container as well as its parameter
}
};
然后您可以使用param
作为普通类型。
答案 1 :(得分:0)
如果您的编译器支持模板模板参数,则可以使用它来解决此问题。有关模板模板参数的详细信息,请查看此链接:http://www.informit.com/articles/article.aspx?p=376878