将导出关键字与模板一起使用 [英] Using export keyword with templates

查看:29
本文介绍了将导出关键字与模板一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,可以使用export"关键字,以便可以通过头文件公开模板类或函数签名,并在库文件中抽象实际实现.
谁能提供一个实用的示例程序来展示如何做到这一点?
使用时有什么缺点或要注意的要点吗?

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?

基于答案的后续问题.正如答案中提到的那样,导出"在 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 在评论中指出的那样,如果在标头中定义了函数的完整特化,则必须将其标记为内联,否则链接器会报错.

(1) 终于找到我的参考为什么我们不能负担得起出口 (PDF) 作者:Tom Plum,由 Edison Design Group 的 Steve Adamczyk、John Spicer 和 Daveed Vandevoorde 审阅,他们最初在 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.

这篇关于将导出关键字与模板一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆