在 C++ 中分离模板接口和实现
这是以下问题的后续问题: 将导出关键字与模板结合使用
正如原始问题“导出”的答案中提到的那样在 C++0x 中已弃用,并且即使对于 C++03,编译器也很少支持。鉴于这种情况,可以通过什么方式隐藏 lib 文件中的实际实现并仅通过头文件公开声明,以便最终用户可以知道公开的 API 的签名是什么,但无法访问实现相同功能的源代码?
This is a follow up question to:
Using export keyword with templates
As mentioned in the answers of the original questions 'export' is deprecated in C++0x and rarely supported by compilers even for C++03. 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我经常注意到的一件事是,很大一部分模板代码实际上并不是那么模板,并且可以移动到非模板函数。
函数模板专门化也被视为常规函数:您可以内联定义它们(并如此标记它们)或在标头中声明它们并在源文件中实现它们。
当然,专业化意味着您知道它将以哪种类型执行......
请注意,您所要求的内容有些矛盾。
模板的真正目标是创建一个“模式”,以便编译器可以为多种不相关的类型生成类和函数。如果隐藏此模式,您如何期望编译器能够生成这些类和函数?
One thing I have often noticed is that a good chunk of template code, is not so template in fact, and can be moved to non-template functions.
It also happens that function template specialization are considered as regular functions: you can either define them inline (and mark them so) or declare them in a header and implement them in a source file.
Of course, specialization means that you know with which type it will be executed...
Note that what you are asking for is somewhat antithetic.
The very goal of template is to create a "pattern" so that the compiler can generate classes and functions for a multitude of unrelated types. If you hide this pattern, how do you expect the compiler to be able to generate those classes and functions ?
您可以在最新的编译器中使用 extern 模板: http://en.wikipedia.org /wiki/C%2B%2B0x#Extern_template
但是,它并不完美,因为它只限制模板实例化。这个想法是将模板声明和实现分离在两个单独的文件中。
然后,当您需要模板时,首先使用 extern 模板,以确保它尚未实例化。然后,对于您需要的每个实例化(一个用于 std::vector,一个用于 std::vector 等),将实例化放入将位于唯一 cpp 中的 typedef 中。
由于它使代码显然更难理解,因此它还不是最好的解决方案。但它确实有效:它有助于最大限度地减少模板实例化。
You can use extern template in most recent compilers : http://en.wikipedia.org/wiki/C%2B%2B0x#Extern_template
However, it's unperfect as it only limit template instantiation. The idea is that you separate the template declaration and implementation in two seperate files.
Then when you need the template, you use extern template first, to make sure it's not instantiated yet. Then for each instantiation you need ( one for std::vector, one for std::vector, etc) , put the instantiation in a typedef that will be in a unique cpp.
As it makes the code clearly harder to understand, it's not the best solution yet. But it does works : it helps minimize template instantiations.
实际上你不能。
只有当您拥有一组特定的专业知识时,您才可以将它们放入图书馆中。基本模板不能放在那里。
另一方面,使用导出并没有隐藏来源。编译器仍然需要它从模板实例化新类。
In practice you cannot.
Only if you have a certain set of specializations, you can put these in a library. The base template cannot be put there.
On the other hand, using export did not hide the source. The compiler still needed it to instantiate new classes from the template.
简而言之,你不能。
export
关键字是一次失败的尝试,旨在实现类似于非源模板库的功能(尽管甚至没有达到二进制代码所达到的混淆程度),并且近期没有替代品。In short, you can't. The
export
keyword was a failed attempt to achieve something akin to non-source template libraries (though not even approaching the level of obfuscation that binary code achieves), and there is no replacement in the offing.