Search code examples
c++header-only

Hide implementation in header-only library


I've tried to use an implementation namespace, but when i 'using namespace' it, it pulls in the whole implementation namespace, rendering it useless.

namespace library {
 namespace implementation {
  //implementation
 }

 using namespace implementation

 //visible identifiers
}

Is there way to hide implementation, without needing to use "implementation::" before using anything from implementation?


Solution

  • rendering it useless

    Well, it's not useless, you just can't using namespace it. You have to explicitly prepend implementation:: to the names in it. For this reason, it might make sense to choose a shorter name (detail is commonly used in c++ projects for this purpose).

    Also, I'd like to point out that header-only and implementation hiding are two somewhat conflicting requirements. If you truly need to hide the implementation, you can't go header-only.