简体   繁体   English

模板类中函数的模板参数

[英]template argument for a function in template class

I have situation like this 我有这样的情况

template<class T> class Vector {
  T *data;
  uint _size, _counter;
public:
  class Iterator;
  template<template<class> class C> Vector(typename C<T>::Iterator it1,
                                           typename C<T>::Iterator it2) {
    data = NULL;
    _size = _counter = 0;
    for(typename C<T>::Iterator it = it1; it != it2 && it != end(); it++)
      push(*it);
  }
};

that is my own Vector class and the constructor mimics behaviour of vector (u can construct it with range of data supplied using interators) but add requirement that the container is to be template of the same type as the one under construction. 这是我自己的Vector类,构造函数模仿了vector的行为(你可以用使用交互器提供的数据范围来构造它),但是要求容器是与正在构造的类型相同类型的模板。 I get error 我收到错误

5.cpp:16:36: error: no matching function for call to 'Vector::Vector(Vector::Iterator, Vector::Iterator)' 5.cpp:16:36: note: candidates are: In file included from 5.cpp:2:0: 5.cpp:16:36:错误:没有匹配函数调用'Vector :: Vector(Vector :: Iterator,Vector :: Iterator)'5.cpp:16:36:注意:候选者是:在包含的文件中5.cpp:2:0:

5.hpp:17:37: note: template class typedef C C> Vector::Vector(typename C::Iterator, typename C::Iterator) 5.hpp:17:37:注意:模板类typedef C C> Vector :: Vector(typename C :: Iterator,typename C :: Iterator)

5.hpp:17:37: note: template argument deduction/substitution failed: 5.hpp:17:37:注意:模板参数扣除/替换失败:

5.cpp:16:36: note: couldn't deduce template parameter 'template class typedef C C' In file included from 5.cpp:2:0: 5.cpp:16:36:注意:无法推断模板参数'template class typedef C C'在5.cpp:2:0中包含的文件中:

5.hpp:11:3: note: Vector::Vector() [with T = int] 5.hpp:11:3:注意:Vector :: Vector()[with T = int]

5.hpp:11:3: note: candidate expects 0 arguments, 2 provided 5.hpp:11:3:注意:候选人需要0个参数,2个提供

5.hpp:7:25: note: Vector::Vector(const Vector&) 5.hpp:7:25:注意:Vector :: Vector(const Vector&)

5.hpp:7:25: note: candidate expects 1 argument, 2 provided 5.hpp:7:25:注意:候选人需要1个参数,2个提供

Need some help in here. 在这需要一些帮助。

In: 在:

 template<template<class> class C> Vector(typename C<T>::Iterator it1,
                                           typename C<T>::Iterator it2) 

the compiler does not deduce type C from typename C<T>::Iterator because it is what is called nondeduced context . 编译器不会从typename C<T>::Iterator推断出类型C ,因为它是所谓的nondeduced context

See §14.8.2.4 Deducing template arguments from a type [temp.deduct.type]: 参见§14.8.2.4从类型[temp.deduct.type]中推导出模板参数:

4 The nondeduced contexts are: 4非弱势语境是:

— The nested-name-specifier of a type that was specified using a qualified-id. - 使用qualified-id指定的类型的嵌套名称说明符。

— A type that is a template-id in which one or more of the template-arguments is an expression that references a template-parameter. - 作为template-id的类型,其中一个或多个模板参数是引用模板参数的表达式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM