C++ 将导出关键字与模板一起使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5416872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using export keyword with templates
提问by Alok Save
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”关键字,以便可以通过头文件公开模板类或函数签名,并在库文件中抽象实际实现。
谁能提供一个实用的示例程序来展示如何做到这一点?
使用时有什么缺点或要注意的要点吗?
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?
编辑:基于答案的后续问题。正如答案中提到的,'export' 在 C++0x 中已被弃用,即使对于 C++03x,编译器也很少支持。在这种情况下,可以通过什么方式隐藏 lib 文件中的实际实现并仅通过头文件公开声明,以便最终用户可以知道公开 API 的签名是什么,但无法访问实现相同的源代码?
回答by Matthieu M.
First of all: most compilers (including gcc, Clang and Visual Studio) do not support the export
keyword.
首先:大多数编译器(包括 gcc、Clang 和 Visual Studio)都不支持export
关键字。
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.
它已在单个前端实现:EDG 前端,因此只有使用它的编译器(Comeau 和 icc)支持此功能。EDG 实现者的反馈非常简单:它花费了我们时间,非常复杂,我们建议不要实现它(1),因此它已在 C++0x 中被删除。
Now, the standard allows (and this is implemented by at least gcc):
现在,标准允许(至少由 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.
注意:正如 Johannes 在评论中指出的那样,如果在头文件中定义了函数的完整特化,则必须将其标记为内联,否则链接器会报错。
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.
(1) 终于找到了 Tom Plum 所著的参考为什么我们不能负担得起出口 (PDF),由 Edison Design Group 的 Steve Adamczyk、John Spicer 和 Daveed Vandevoorde 审阅,他们最初在 EDG 前端实现了它。
回答by ltc
Export has been removed from the C++ standard. Do not use it.
导出已从 C++ 标准中删除。不要使用它。
回答by templatetypedef
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.
很难提供示例程序,因为几乎没有编译器支持导出。g++ 会报告一个警告说它不受支持,并且 IIRC 它甚至不能在 Visual Studio 中编译。此外,导出在 C++0x 中已被弃用,这意味着未来的编译器不太可能完全支持它。
For a discussion of how to use export in the few compiles that do support it (namely Comeau C++), check out this linkwhich also goes into why export is hard to implement.
有关如何在支持导出的少数编译(即 Comeau C++)中使用导出的讨论,请查看此链接,该链接还介绍了导出难以实现的原因。
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.
如果这被认为是主要的反出口咆哮,我们深表歉意。我保证我不讨厌出口!它只是没有得到广泛的支持,你不能真正依赖它作为程序员。
回答by CashCow
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
http://msmvps.com/blogs/vandooren/archive/2008/09/24/c-keyword-of-the-day-export.aspx
You are better off instantiating your templates.
你最好实例化你的模板。
回答by Tavison
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.
做你想做的事情的唯一方法就是像上面所说的那样完全专业化。但更重要的是,如果你看不到库的源代码,那么你就无法编译它。这意味着您不能从它接受动态内存,因为不能保证您将使用匹配的删除到他们的新。例如,如果我的代码是 debug 并且库是 release,则删除器将与新的不匹配。您可以使用 shared_ptr 并提供删除器,但那是 TR1 并且没有导出。
回答by pconnell
C++11 now has 'extern templates' which are already well supported by modern compilers.
C++11 现在有“extern 模板”,现代编译器已经很好地支持这些模板。