简体   繁体   English

在C ++中分离模板接口和实现

[英]Separating template interface and implementation in C++

This is a follow up question to: Using export keyword with templates 这是一个后续问题: 将export关键字与模板一起使用

As mentioned in the answers of the original questions 'export' is deprecated in C++0x and rarely supported by compilers even for C++03. 正如在原始问题的答案中所提到的,'export'在C ++ 0x中已被弃用,即使对于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? 鉴于这种情况,可以用什么方式隐藏lib文件中的实际实现,只是通过头文件公开声明,以便最终用户可以知道公开的API的签名是什么,但是无法访问实现相同的源代码?

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. 编译器仍然需要它来从模板中实例化新类。

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. export关键字是尝试实现类似于非源模板库的尝试失败(尽管甚至没有接近二进制代码实现的混淆程度),并且没有替代。

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 ? 如果隐藏此模式,您希望编译器如何能够生成这些类和函数?

You can use extern template in most recent compilers : http://en.wikipedia.org/wiki/C%2B%2B0x#Extern_template 您可以在最新的编译器中使用extern模板: 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. 然后,当您需要模板时,首先使用extern模板,以确保它尚未实例化。 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. 然后对于你需要的每个实例化(一个用于std :: vector,一个用于std :: vector等),将实例化放在一个将在唯一cpp中的typedef中。

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. 但它确实有效:它有助于最小化模板实例化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM