1

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. 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.hpp:17:37: note: template class typedef C C> Vector::Vector(typename C::Iterator, typename C::Iterator)

5.hpp:17:37: note: template argument deduction/substitution failed:

5.cpp:16:36: note: couldn't deduce template parameter ‘template class typedef C C’ In file included from 5.cpp:2:0:

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

5.hpp:11:3: note: candidate expects 0 arguments, 2 provided

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

5.hpp:7:25: note: candidate expects 1 argument, 2 provided

Need some help in here.

4

1 回答 1

4

在:

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

编译器不会从中推断类型Ctypename C<T>::Iterator因为它是所谓的非推断上下文

请参阅 §14.8.2.4 从类型 [temp.deduct.type] 推导模板参数:

4 未推断的上下文是:

— 使用限定 ID 指定的类型的嵌套名称说明符。

— 一种模板 ID 类型,其中一个或多个模板参数是引用模板参数的表达式。

于 2013-04-18T21:48:11.407 回答