問題描述
據我所知,可以使用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.
這篇關于將導出關鍵字與模板一起使用的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!