0

C++ Compiler needs to have access to the implementation of the methods in order to instantiate a template class. Hence we need to either include the definitions of a template in the header file that declares that template or define them in header files. But I want to hide the implementation (definitions) of my functions from my users due to many reasons and want to ensure that they have only access to the function declarations. Is it possible to do so while using templates in C++?

Soo
  • 885
  • 7
  • 26

1 Answers1

3

This is usually done (for eg) in boost by creating a directory hierarchy impl and detail.

impl will usually have the implementatios for the exposed API member function while inside the detail as you might have guessed, has the gritty details which you do not want to expose. This system is fairly well understood by the C++ developers.

As an example:

my_service.hpp
impl/my_service.hpp
impl/my_service.ipp  ( OR )
detail/my_service.ipp

So, the idea is to split the header files and provide the users to only include your top level header file which will internally include the other files.

But nobody is stopping the users to include the impl header file as well directly. If you want to avoid that too, then you can do something on the lines of Prevent header from being included in some files, in compilation time?

Community
  • 1
  • 1
Arunmu
  • 6,837
  • 1
  • 24
  • 46