1
votes

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.

1
What's that template<template<class> class C>?Alexander Shukaev
constructor is template method with parameter C that is itself a template with one parameter name irrevelant.lord.didger
it != end(). That can't be right.jrok
I presume you are defining the class Iterator; below the function definition?indeterminately sequenced
It looks like you have a case of non-deducible context here. In a nutshell, you cannot deduce C out of C<T>::iterator. It is in fact useless, you are not even mention it in the body of the function. Parameterize by the iterator type, never by the container type.n. 1.8e9-where's-my-share m.

1 Answers

4
votes

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.

See §14.8.2.4 Deducing template arguments from a type [temp.deduct.type]:

4 The nondeduced contexts are:

— The nested-name-specifier of a type that was specified using a 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.