[Effective-cpp] Item 35: Familiarize yourself with the language standard
Terje Slettebø
tslettebo at chello.no
Tue Jun 10 09:06:01 EDT 2003
>From: "Terje Slettebø" <tslettebo at chello.no>
> To give you a hint of the power of generic programming and concepts:
> Consider the algorithm that was presented earlier in this thread:
>
> template<class T>
> T * find( T * begin, T *end, cont T& value )
> {
> while (begin != end && *begin != value) ++begin;
>
> return begin;
> }
>
> Because it doesn't assume the type of T, it can be used with any type that
> "models the concept", i.e. that conforms to the concept requirement. In
this
> case, T is required to be a model of the concept InputIterator. Whatever
> type conforms to this, can be used. That means that it must provide a
"!=",
> "*", and "++" operator, etc.
Actually, looking at the code again, I realise that it may not be used like
that, as it requires fundamental (built-in) types However, if you write it
as follows, what I said above holds (substitute "InputIterator" for "T" in
the quote above):
template<class InputIterator, class T>
InputIterator find(InputIterator begin, InputIterator end, const T &value)
{
// The rest of the code is the same
while (begin != end && *begin != value; ++begin;
return begin;
}
Regards,
Terje
More information about the Effective-cpp
mailing list