Naming the implementation version of an interface function
- by bolov
When I need to write an implementation version of an interface function, I put the implementation function within a impl namespace, but with the same name as the interface function. Is this a bad practice? (the same name part, the namespace part I am confident it’s more than OK). For me, who I write the code, there is no confusion between the two, but I want to make sure this isn’t confusing for someone else. One other option would be to append impl suffix to the function name, but since it is already in a separate namespace named impl it seems redundant. Is there an idiomatic way to do this?
E.g.:
namespace n {
namespace impl {
// implementation function (hidden from users)
// same name, is it ok?
void foo() {
// ...
//sometimes it needs to call recursively or to call overloads of the interface version:
foo(); // calls the implementation version. Is this confusing?
n::foo(); // calls the interface version. Is this confusing?
// ...
} // namespace impl
// interface function (exposed to users)
void foo() {
impl::foo();
}
} // namespace n