Why do generic programming designs prefer free functions over member functions?
boost, c++, generics
Solution
The main motivation is that preferring nonmember nonfriend functions helps keep classes as concise as possible. See Herb Sutter's article here: http://www.gotw.ca/publications/mill02.htm.
This article also contains the answer to the other part of your question, where to put the corresponding get function. It's a feature of C++ called Argument Dependent Lookup (ADL). From Herb Sutter, who calls it Koenig lookup although this name is controversial (see comments below):
Koenig lookup says that, if you supply a function argument of class type, then to find the function name the compiler is required to look, not just in the usual places like the local scope, but also in the namespace (here NS) that contains the argument's type.
Here's an example:
namespace MyNamespace {
class MyClass {... };
void func(MyClass);
}
int main(int aArgc, char* aArgv[]) {
MyNamespace::MyClass inst;
func(inst); // Ok, because Koenig says look in the argument's namespace for func
}
So in short, you just declare the get function in the same namespace as your class.
Be aware that this doesn't work for templated functions if you have to supply the template parameters explicitly -- see this post: https://stackoverflow.com/a/2953783/27130
Problem
I recently got introduced the design of generic programming libraries like STL, boost::graph, boost PropertyMaps http://www.boost.org/doc/libs/1_54_0/libs/property_map/doc/property_map.html What is the rationale behind using free functions like get(PropertyMap, key) over member functions like PropertyMap.get(key)? I understand that the most generic form of these functions are defined in the "boost" namespace. Suppose I define a new PropertyMap in my namespace "project", what is the best place to define it's corresponding "get" function? "boost" or "project"