Reading a book A tour of c++ (second edition), 2018, I didn't understand an explanation about templates (I'll explain below).
Two function signatures are given for a find_all
function that returns a vector
of iterator
s to all occurrences of a given value in a given container
.
#1 :
template<typename C, typename V>
vector<typename C::iterator> find_all(C& c, V v);
#2 :
template<typename T>
using Iterator = typename T::iterator;
template<typename C, typename V>
vector<Iterator<C>> find_all(C& c, V v) ;
Both can be used like this :
string m {"Mary had a little lamb"};
for (auto p : find_all(m,'a')) // here p is a string::iterator
cout << *p << endl; // spoiler alert : this will print 'a's
The #2 using alias template is said to
hide the implementation detail by introducing a type alias for Iterator
by the author.
Although I think I understand both usage of templates, I don't understand why would #2 "hide the implementation detail" and why it is preferred ... Can anyone explain me ?
Thanks !
Ps : I didn't provide the post with the definition of the functions (same for both signatures) because I think it isn't relevant, but I'll add it if anyone needs it.