Free functions versus member functions

Daniel Keep daniel.keep.lists at gmail.com
Sun Oct 14 16:07:48 PDT 2007



Kevin Bealer wrote:
> I guess what I'm thinking is that find(a.begin(), a.end(), value) could be written, and string::find(...) could also be written.  If the most efficient find() is the external one, then string::find() could just use that.  If not, it can use an different internal version.
> 
> My thinking is that users wanting speed can say s.find(...) and users wanting flexibility could say find(x.begin(), x.end(), ...) where x is any type.  Maybe there's a nicer way to do this (I guess people use template specializations?)
> 
> Kevin
> 

Yes, Walter calls it "uniform function call syntax" or somesuch.  Basically:

  s.find(...)

and

  find(s, ...)

become equivalent.  This means that you can have a global find functions
like thus:

  size_t find(T,U)(T collection, T thing_to_find) { ... }
  size_t find(T)(ICollection collection, T thing_to_find) { ... }

And optimised member functions for particular implementations

  struct CrazyCollection(T)
  {
    size_t find(T thing_to_find) { ... }
  }

And they all get invoked the same way.  The user no longer has to care
where the function was written, just that it exists.

	-- Daniel



More information about the Digitalmars-d mailing list