vector作为模板参数.所有的地狱都失去了

std::vector as template parameter... all hell breaks lose

本文关键字:地狱 失去 参数 vector      更新时间:2023-10-16

我马上就要爆炸了…请有人指出目前这里有什么问题:

template <typename TType, template <typename ...> class Container, class Comparer>
Container<TType>* sort(const Container<TType>& container) {
    ...
}
当我尝试用std::vector作为容器形参来调用这个函数时,问题就出现了。我得到以下错误:
main.cpp:24:34: error: no matching function for call to 'func()'
main.cpp:24:34: note: candidate is:
main.cpp:14:6: note: template<class T, template<class ...> class Container> void func()
main.cpp:14:6: note:   template argument deduction/substitution failed:

我是这样命名的:

std::vector<int>* m(sort<int, std::vector<int>, Comparer>(m));

当我从函数中删除模板模板参数时,它工作,但不与它一起…我使用的是MinGW附带的最新的c++编译器。IDE是NetBeans 7.3,不过这应该不会有太大影响。编译器参数为:

-std=c++11 -Wall -pedantic

谢谢每一个帮助,——乔伊

您应该提供一个模板,而不是从模板创建的特定类型。正确的调用应该是:

sort<int, std::vector, Comparer>(m)

注意,sort本身提供了Container的模板参数,就像const Container<TType>&一样。显然将Container设置为std::vector<int>是没有意义的;你会要求编译器做std::vector<int><int>