2

I have read the Why can templates only be implemented in the header file? and Why can’t I separate the definition of my templates class from its declaration and put it inside a .cpp file?

If I create the templates then I am to provide access also to their cpp-files additionally to their h-files, or write the definitions directly in the header file.

Therefore, if I want to allow to use fully my templates in other applications, then I can't hide their implementation from outside eyes (for protection of intellectual property). Am I right?

Community
  • 1
  • 1
Andrey Bushman
  • 11,712
  • 17
  • 87
  • 182
  • Should your templates be usable with user types, or is it ok to restrict them to a small predefined list of types? – Marc Glisse Dec 03 '15 at 12:55

2 Answers2

6

In general you're right... the implementation must be exposed.

If your client only needs to instantiate them for a finite set of specific types that they can list for you, you can provide them with a pre-compiled object/library containing the implementations of the instantiations for just those types: see https://isocpp.org/wiki/faq/templates#separate-template-fn-defn-from-decl

Obfuscation is another possibility - let them see the code, but make it confusing and unmaintainable.

If neither of those options suit, consider whether you can provide a templated adapter that creates a run-time polymorphic interface over their user-provided type, capturing the specific set of functions your algorithms need. Accept those adapters as a front-end to your code. This does have runtime costs.

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
2

Intellectual property is mostly protected by legal means, not technical ones.

(e.g. the technical possibility to read some header files do not give me the right to use it, or to copy its code elsewhere)

However, you might consider code obfuscation techniques. You might even customize your recent GCC for that purpose, i.e. write your MELT free software extension. It could mean weeks (or months) of work.

Alternatively, consider publishing your header-only template C++ library as free software... (perhaps with GPL license).

But IANAL. You should ask your lawyer.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547