39

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?

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • 1
    I don't know that it's supported by all compilers as of yet. I think Comeau is one of the few that has it. – jonsca Mar 24 '11 at 09:10
  • 1
    [This C++ FAQ-Lite entry](http://www.parashift.com/c++-faq-lite/templates.html#faq-35.14) seems to answer your question. – Björn Pollex Mar 24 '11 at 09:12
  • Hasn't export been deprecated in C++0x ? Moreover it is unavailable in C++03 (except in the Comeau compiler). – Alexandre C. Mar 24 '11 at 09:12
  • @Alexandre C: it was considered for deprecation (http://herbsutter.com/2009/10/23/deprecating-export-considered-for-iso-c0x/) but I didn't follow the whole thing. – Matthieu M. Mar 24 '11 at 09:37
  • 4
    "As mentioned in the answers 'export' is deprecated in C++0x" those answers are not correct if they say such. The keyword was completely removed from C++0x. – Johannes Schaub - litb Mar 24 '11 at 15:32

7 Answers7

49

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.

Matthieu M.
  • 287,565
  • 48
  • 449
  • 722
  • 1
    That's actually not the paper that led to the deprecation in C++0x. (it's a 2003 paper) In fact, this proposal was voted down with quite a large margin (IIRC, about 2/3rd in favor of keeping `export` in the informal vote, didn't make it to formal voting) – MSalters Mar 24 '11 at 09:33
  • @MSalters: I never intended to suggest that, I'll make it clearer :) – Matthieu M. Mar 24 '11 at 09:36
  • 1
    "to declare a specialized version of a template function in a header to define this specialization in a single source file". Huh. That's entirely what the spec requires to be possible. Explicit specializations of function templates are functions (not templates), and actually *cannot* reliably be defined in headers unless they are *inline* (otherwise, multiple definition errors occur). Am i misunderstanding you? – Johannes Schaub - litb Mar 24 '11 at 15:41
  • ...and the result if the overwhelming eachother including inline templates of the C++... catastrophe. :-( – peterh Jun 17 '16 at 03:00
  • Export is back, with C++ modules (C++20) - https://en.cppreference.com/w/cpp/language/modules – ProfNandaa Sep 19 '22 at 11:58
  • 1
    @ProfNandaa: Good point; added a disclaimer to make it clear this answer (and question) discuss pre-C++20 `export` to avoid confusion. – Matthieu M. Sep 19 '22 at 12:01
15

Export has been removed from the C++ standard. Do not use it.

peterh
  • 11,875
  • 18
  • 85
  • 108
ltc
  • 3,313
  • 1
  • 28
  • 26
  • 1
    `The keyword is unused and reserved.` looks more like "it's not implemented yet" rather than "it's obsolete". Thanks anyway – rr- Nov 25 '14 at 20:22
  • 4
    Actually, in c++ 2003 the functionality export keyword was defined. Basically no compilers ever implemented that functionality. In c++ 2011 that functionality was removed from the c++ standard. The export keyword is still reserved but has no functionality. The poster however was clearly asking about the c++ 2003 functionality associated with the export keyword which has been removed from C++. – ltc Nov 26 '14 at 17:45
  • I see, that explains why it's kept unused. – rr- Nov 26 '14 at 18:45
  • 2
    And it's back again with Modules. – Dan M. Mar 26 '18 at 19:20
5

It's difficult to provide a sample program because almost no compilers support export. g++ will report a warning saying that it's not supported, and IIRC it doesn't even compile in Visual Studio. Moreover, export is deprecated in C++0x, meaning that it's unlikely that future compilers will support it at all.

For a discussion of how to use export in the few compiles that do support it (namely Comeau C++), check out this link which also goes into why export is hard to implement.

And apologies if this comes across as a major anti-export rant. I promise that I don't hate export! It's just not widely supported and you can't really rely on it as a programmer.

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
1

The reasons many compiler vendors did not support it is that even when it works it does not do so the way programmers would expect.

The best article I found on the issues is here:

http://msmvps.com/blogs/vandooren/archive/2008/09/24/c-keyword-of-the-day-export.aspx

You are better off instantiating your templates.

CashCow
  • 30,981
  • 5
  • 61
  • 92
0

C++11 now has 'extern templates' which are already well supported by modern compilers.

pconnell
  • 4,551
  • 1
  • 14
  • 9
0

I read an article with the title something like Export is not supported and it wouldn't do what you want anyway.

The only way to do what you want is to fully specialize as has been said. But more than that, if you can't see the source code of a library then you can't compile it. This means you cannot accept dynamic memory from it since there is no guarantee you will use the matching delete to their new. For example, if my code is debug and the library is release the deleter will not match the new. You could use shared_ptr and provide a deleter, but that's TR1 and does not have export.

Tavison
  • 1,523
  • 1
  • 11
  • 19
0

The export keyword is supported in C++20 and is used with the modules language feature (https://en.cppreference.com/w/cpp/language/modules).