I've recently made a change stylistically and wanted to see how other c++ programmers felt about it and if there were any downsides to it.
Essentially, when I needed a utility function which doesn't need access to a given class member, what I used to do was something like this:
file.h
class A {
public:
// public interface
private:
static int some_function(int);
};
file.cpp
int A::some_function(int) {
// ...
}
But more recently, I have been preferring to do something more like this:
file.cpp
namespace {
int some_function(int) {
}
}
// the rest of file.cpp
Here's my thought process:
- it's one less file the edit
- simply having the function be listed in the header file can hint at implementation details which have no need to be exposed (even if not publically available).
- if the prototype of the function needs to change, only one file needs to be recompiled.
The last one is the most compelling to me. So my question is: are there any downsides to this?
They are functionally equivalent for most purposes that I can think of. It kind of seems to me that a private static function can almost always be converted to a free function in an anonymous namespace.
EDIT: One thing that comes to mind is that a private static function would have access to private members if given a pointer to an object to operate on, but if that's the case, why not make it a non-static member?
What do you guys think?