10

This is a follow up question to: Using export keyword with templates

As mentioned in the answers of the original questions 'export' is deprecated in C++0x and rarely supported by compilers even for C++03. Given this situation, in what way can one hide actual implementations in lib files and just expose declarations through header files, So that end user can know what are the signatures of the exposed API but not have access to the source code implementing the same?

Community
  • 1
  • 1
Alok Save
  • 202,538
  • 53
  • 430
  • 533

4 Answers4

8

In practice you cannot.

Only if you have a certain set of specializations, you can put these in a library. The base template cannot be put there.

On the other hand, using export did not hide the source. The compiler still needed it to instantiate new classes from the template.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
  • 1
    it's also possible to only declare the base template, in which case instantiating with types for which there is no specialization provided is impossible. – Matthieu M. Mar 24 '11 at 10:15
  • Certain specializations basically mean special functions for each case(just similar to any API...No use of power of templates). – Alok Save Mar 27 '11 at 16:24
  • Matthieu M: Does this mean that if I am using templates for development I have to expose all of my developed source code to the user(read 3rd party). Theres no way to enforce intellectual rights? – Alok Save Mar 27 '11 at 16:27
  • 1
    @Als - if you have a templated interface, then yes the customers must have access to the source for those templates. Your options depend a bit on what the software does, but if the templates are instantiated just for a small number of types, you could provide those specializations in a library, like Matthieu suggested. Or you could possibly provide an non-template API. There are lots of ways to enforce intellectual rights, a contract for example, and a bunch of lawyers. :-) On the other hand, the only existing implementation of the `export` keyword still needs the source, so that is no help. – Bo Persson Mar 27 '11 at 17:53
2

In short, you can't. The export keyword was a failed attempt to achieve something akin to non-source template libraries (though not even approaching the level of obfuscation that binary code achieves), and there is no replacement in the offing.

Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
1

One thing I have often noticed is that a good chunk of template code, is not so template in fact, and can be moved to non-template functions.

It also happens that function template specialization are considered as regular functions: you can either define them inline (and mark them so) or declare them in a header and implement them in a source file.

Of course, specialization means that you know with which type it will be executed...

Note that what you are asking for is somewhat antithetic.

The very goal of template is to create a "pattern" so that the compiler can generate classes and functions for a multitude of unrelated types. If you hide this pattern, how do you expect the compiler to be able to generate those classes and functions ?

Matthieu M.
  • 287,565
  • 48
  • 449
  • 722
1

You can use extern template in most recent compilers : http://en.wikipedia.org/wiki/C%2B%2B0x#Extern_template

However, it's unperfect as it only limit template instantiation. The idea is that you separate the template declaration and implementation in two seperate files.

Then when you need the template, you use extern template first, to make sure it's not instantiated yet. Then for each instantiation you need ( one for std::vector, one for std::vector, etc) , put the instantiation in a typedef that will be in a unique cpp.

As it makes the code clearly harder to understand, it's not the best solution yet. But it does works : it helps minimize template instantiations.

Klaim
  • 67,274
  • 36
  • 133
  • 188