Using export keyword with templates

Attention: This answer is about the historical use of export pre-C++20; C++20 repurposes the keyword for use in modules.

First of all: most compilers (including gcc, Clang and Visual Studio) do not support the export keyword.

It has been implemented in a single front-end: the EDG front-end, and thus only the compilers that use it (Comeau and icc) support this feature. The feedback from the implementers at EDG was extremely simple: it took us time, was extremely complicated, we recommend not to implement it (1), as a consequence it has been dropped in C++0x.

Now, the standard allows (and this is implemented by at least gcc):

  • to declare a specialized version of a template function in a header
  • to define this specialization in a single source file

and to have it behave as you’d expect from a regular function.

Note: as Johannes points out in a comment, if a full specialization of a function is defined in a header, it must be marked as inline otherwise the linker will complains.

EDIT:

(1) Finally found my reference Why can’t we afford export (PDF) by Tom Plum, reviewed by Steve Adamczyk, John Spicer, and Daveed Vandevoorde of Edison Design Group who originally implemented it in the EDG front end.

Leave a Comment