In a Stroustrup "A Tour of C++" he wrote example of find_all as
template<typename C, typename V>
vector<typename C::iterator> find_all(C& c, V v)
// find all occurrences of v in c
{
vector<typename C::iterator> res;
for (auto p = c.begin(); p!=c.end(); ++p)
if (∗p==v)
res.push_back(p);
return res;
}
What is typename C::iterator in template<typename C, typename V> vector<typename C::iterator> find_all? I haven't seen <> being both before and after function name. What is this construction and how it words? In book he wrote
The typename is needed to inform the compiler that C’s iterator is supposed to be a type and not a
value of some type, say, the integer 7. We can hide this implementation detail by introducing a type
alias (§6.4.2) for Iterator:
template<typename T>
using Iterator = typename T::iterator;
// T’s iterator
template<typename C, typename V>
vector<Iterator<C>> find_all(C& c, V v)
// find all occurrences of v in c
{
...
Which doesn't make things clearer. I understand what using Iterator = typename T::iterator; is, but it doesn't explain second <> usage.
Aucun commentaire:
Enregistrer un commentaire