I am reading Effective C++, Item 41 with the title of "Understanding implicit interfaces and compile-time polymorphism", It give this example and next an explanation but I don't understand this part.
template<typename T>
void doProcessing(T& w)
{
if (w.size() > 10 && w != someNastyWidget) {
...
..., T must support a size member function, ....., But this member function need not return an integral type. It need not even return a numeric type. For that matter, it need not even return a type for which operator
>is defined! All it needs to do is return an object of some type x such that there is an operator>that can be called with and object of type x and an int ...
Could you please explain what it is about and give more examples?
It means that the
T::size()function can return anything which can be compared (using>) to anintvalue.Lets look at three examples:
Return an
int:Return an object which can be converted to an
int:Return an object which can be compared with
>to anint:While it should be quite clear that the first variant can be used, the other two can also be used. That is because they, one way or another, returns something which can be compared to an
intvalue.If we "expand" the three variants:
w.size() > 10is simply like it is.w.size() > 10will bew.size().operator int() > 10. Here theMySizeType::operator int()conversion function will be used to convert theMySizeTypeobject to anintvalue that can be compared.w.size() > 10will bew.size().operator>(10). Here theMyOtherType::operator>()function will be used for the comparison itself.Reference