I was looking at the request parser from the boost::asio example and I was wondering why the private member functions like is_char()
are static
? :
So is there a reason why these functions are static?
Non-static
member functions have a hidden additional parameter called this
. Passing this doesn't come for free, so making a private
function static
can be seen as a means of optimization.
But it can also be seen as a means of expressing your requirements/design in your code: If that function doesn't need to refer to any member data of the class, why should it be a non-static
member function?
However, changing the type of any member function, public
or private
, static
or not, will require all clients to recompile. If this needs to be done for a private
function which those clients can never use, that's a waste of resources. Therefore, I usually move as many functions as possible from the class' private parts into an unnamed namespace in the implementation file.