The existing answers are great and, of course, for the standards committee were vital for this purpose. But there is another problem that, in my opinion, is important enough to mention.
With free features, you have the ability to change the interface without changing the class definition. You can do any type of "gettable" simply by specializing in global get . With the member function, you will need to directly modify the class.
This is one of the reasons why the range-based for built on std::begin/std::end instead of looking for member functions. std::begin/end specialized for array types, so you can use for based on a range with arrays. You can use it with any container, even those that do not have begin/end functions. You can specialize it, for example, for LibXML2 element types, so you can use for based on the xmlElement* range.
You cannot do this if they must be member functions.
In C ++, free functions are the natural interface for many operations that can be performed for different classes.
Nicol Bolas Oct 16 2018-12-12T00: 00Z
source share