In the book - the C++ Primer 3rd edition, Stanley Lippman has introduced something about the compilatio model . He introduced two models.
- inclusion compilation model
- separation compilation model
with inclusion compilation model, the definition of the function template is included in the header file, while every source file that instantiates the function template is required to include the header file.
for space 's reason, I will not introduce the inclusion model in details.
He also introduced the separation model .
the quintessence/gist/marrow/essence/heart of the separation model is in the header file there is only the function template's declaration, and definition of the function template can goes into a separate source file. Client to the template function can include the header file and the the separate source file will have an export keyword to qualify the function's definition.
it is suppose to be like this;
// model2.h
template <typename Type> Type min2(Type t1, Type t2);
// model2.C
export template <typename Type>
Type min2(Type t1, Type t2) {
return t1 < t2 ? t1 : t2;
}
and if you wan to use the template function , say in user.C , do this
#include "model2.h"
int main2(int argc, char* argv[])
{
int i, j;
double dobj = min2(i, j);
return 0;
}
!!!BUT!!!!
However, the prolem is that the compiler that I tried, inlcuding the vc++ 11 or the gnu c++ does not support the export keyword, so I think it is not supported to have separation compilation model .
Stanley Lippman 在《C++ Primer 第三版》中介绍了两种编译模型:包含编译模型和分离编译模型。分离编译模型中,函数模板的定义位于头文件中,而使用模板函数的客户端只需包含头文件即可,模板函数的实现则通过一个带有export关键字的单独源文件提供。然而,由于某些编译器不支持export关键字,分离编译模型在实际应用中存在限制。
6849

被折叠的 条评论
为什么被折叠?



