Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using export keyword with templates

As i Understand "export" keyword can be used so that one can expose template classes or function signatures through an header file and abstract the actual implementation in a library file.
Can anyone please provide a practical sample program which shows how to do this?
Are there any disadvantages or important points to note while using this?

EDIT: A follow up question based on the answers. As mentioned in the answers 'export' is deprecated in C++0x and rarely supported by compilers even for C++03x. 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?

like image 242
Alok Save Avatar asked Mar 24 '11 09:03

Alok Save


People also ask

Is it possible to export a template?

You can export the template that was used to deploy existing resources. The template you get is exactly the one that was used for deployment.

Is export a keyword in C++?

C++'s export keyword was originally meant to permit the separation of the definition of a template from its usages. It proved to be incredibly difficult to implement, and the “ export templates” C++ feature was dropped in C++11, having never seen widespread usage.

Which keyword is used to define template?

5. Which keyword is used for the template? Explanation: C++ uses template reserved keyword for defining templates.

Is template a keyword in C++?

C++ adds two new keywords to support templates: 'template' and 'typename'. The second keyword can always be replaced by the keyword 'class'.


1 Answers

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.

like image 158
Matthieu M. Avatar answered Oct 10 '22 18:10

Matthieu M.