问题描述
据我所知出口关键字可以使用,使人们可以通过一个头文件和抽象的实际执行在一个库文件揭露模板类或函数签名。结果
谁能请提供实用的示例程序,它显示了如何做到这一点?
在使用这一点时是否有任何缺点或重要的要点?
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?
编辑:基于答案的后续问题。如答案中提到的,export在C ++ 0x中已弃用,并且甚至对于C ++ 03x也很少被编译器支持。鉴于这种情况,用什么方式可以在一个隐藏的lib文件实际实现,只是揭露过的头文件中的声明,使最终用户能够知道什么是公开的API的签名,但无法访问源代码的情况一样吗?
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?
推荐答案
首先:大多数编译器(包括gcc,Clang和Visual Studio)不支持 export
First of all: most compilers (including gcc, Clang and Visual Studio) do not support the export
keyword.
它已经实现在单个前端:EDG前端,因此只有使用它的编译器Comeau和icc)支持此功能。从EDG实施者的反馈是非常简单的:我们花了时间,是极其复杂的,我们建议不要执行它的(1),结果它已被C ++ 0x中删除。
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.
现在,标准允许(至少由gcc实现):
Now, the standard allows (and this is implemented by at least gcc):
- 要在头声明一个模板函数的专用版本
- 在一个源文件来定义这种专业化
并且它的行为方式正如你从一个常规函数中所期望的那样。
and to have it behave as you'd expect from a regular function.
注意:正如Johannes在注释中指出的那样,如果一个函数的完整专业化的标题定义,它必须为inline否则连接器会抱怨的
修改:
(1)最后找到我的参考 by Tom Plum,由Steve Adamczyk,John Spicer和Daveed Vandevoorde的Edison设计集团评论,他最初在EDG中实现它
(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.
这篇关于使用导出关键字与模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!