Naming the implementation version of an interface function

Posted by bolov on Programmers See other posts from Programmers or by bolov
Published on 2014-06-02T15:12:12Z Indexed on 2014/06/02 15:56 UTC
Read the original article Hit count: 132

Filed under:
|

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

© Programmers or respective owner

Related posts about c++

Related posts about coding-style