On 6/9/2015 1:55 PM, Doug Mika wrote:
>[..]
> The following for example will not work: (but why do we need typename C here and just C previously?)
>
> //this won't compile, "typename" is needed, but why is it needed here but
> //not previously?
> template<typename C, typename V>
> vector<C::iterator> find_all(C& c, V v)
> {
> vector<C::iterator> res;
> for(auto p = c.begin(); p!=c.end(); ++p)
> if(*p==v) res.push_back(p);
> return res;
> }
>
A bit impatient, aren't you?
It's good that you tried this. It's bad that you don't actually reach
for a decent book and study instead of just posting here. Find a copy
of "C++ Templates" by Vandevoorde and Josuttis, and perhaps you will get
more questions answered than raised by your attempts.
C::iterator is what's known as a "dependent name". In order for the
compiler to be able to do the syntactic analysis of the template, it has
to know what 'C::iterator' is. We need to tell it that it's a typename,
and not, say, a non-static member function. Otherwise the compiler will
need to instantiate your template before it can do the syntactic
analysis, which is not good, and probably leads to chicken-and-egg type
of problems.