0

so I have created a template class which is not trivial and everything works as expected. I want to use this class as a kind of library and hide the implementation details. The code looks something like this:

 //Foo.hpp     
 #include "Header1.hpp"
 #include "Header2.hpp"

 #include "IFoo.hpp"
 ...

 template<typename T>
 class Foo : public IFoo{
   virtual ~Foo() = default;
   void method1(const T &t) override{
     //Do something
   }
  //...
 };

Now I wanted to have some kind of factory to hide the concrete implementation:

//Factory_foo.hpp
#include "Foo.hpp"

template<typename T>
std::unique_ptr<IFoo> create() {
  return std::make_unique<Foo<T>>();
}

So even, if the application code would include "Factory_foo.hpp" I cannot prevent the application code to include Header files which are necessary for the concrete implementation. Normally, I would put the method "create()" into the source file but this is not possible because the template types can be anything.

Any ideas how I can hide implementation details in this case?

Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • 2
    Short answer - you can't really hide it, because of [this issue](https://stackoverflow.com/questions/495021/). But, you could move the implementation into a separate file that is `#include`'d by `Factory_foo.hpp` – Remy Lebeau Sep 14 '22 at 18:10
  • 1
    If you know _all_ possible template arguments beforehand, you can use [explicit instantiation](https://en.cppreference.com/w/cpp/language/class_template) to “hide” your implementation. – Andrej Podzimek Sep 14 '22 at 18:26

0 Answers0