std :: vector作为模板参数...所有的地狱打破输 [英] std::vector as template parameter... all hell breaks lose

查看:283
本文介绍了std :: vector作为模板参数...所有的地狱打破输的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很快爆炸...有人请指出目前的错误:

 模板< typename TType,template< typename ...> class Container,class Comparer> 
Container< TType> * sort(const Container< TType>& container){
...
}

问题出现时,我试图调用此函数与std :: vector作为其Container参数。我得到以下错误:

  main.cpp:24:34:错误:没有匹配函数调用'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:模板参数扣除/替换失败:


$ b b

下面是我如何调用它:

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

当我从函数中删除模板模板参数时,它工作,使用MinGW附带的最新g ++编译器。 IDE是NetBeans 7.3,不应该影响太多。编译器参数为:

  -std = c ++ 11-Wall -pedantic 



感谢您的帮助,
- Joey

解决方案>

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

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

请注意, sort 本身提供 Container ,如 const Container< TType>& 。清楚地将 Container 设置为 std :: vector< int> 你会要求编译器执行 std :: vector< int>< int>


I'm exploding soon... Someone please point out what is wrong in here currently:

template <typename TType, template <typename ...> class Container, class Comparer>
Container<TType>* sort(const Container<TType>& container) {
    ...
}

The problem comes when I try to call this function with std::vector as its Container parameter. I get the following errors:

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:

And here's how I'm trying to call it:

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

When I remove the template template parameter from the function it works, but not with it... I'm using the latest g++ compiler that comes with MinGW. The IDE is NetBeans 7.3 tho that shouldn't affect much. Compiler arguments are:

-std=c++11 -Wall -pedantic

Thanks for every help, - Joey

解决方案

You're supposed to provide a template, not a specific type created from a template. The correct call would be:

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

Note that sort itself is providing the template arguments for Container, as in const Container<TType>&. Clearly setting Container to std::vector<int> makes no sense; you'd be asking the compiler to do something like std::vector<int><int>

这篇关于std :: vector作为模板参数...所有的地狱打破输的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆