简体   繁体   English

在模板头文件的末尾包含.cpp

[英]Including .cpp at end of template header file

I was reading an older data-structures book and it said that when you're doing template class programming you should include the .cpp at the end of the .h file. 我正在阅读一本较旧的数据结构书,它说当你进行模板类编程时,你应该在.h文件的末尾包含.cpp。

As far as I know you have to do full function implementations in your .h file for any template class member functions - it's due to the way the template compiler works. 据我所知,您必须在.h文件中为任何模板类成员函数执行完整的函数实现 - 这是由于模板编译器的工作方式。

The only functions I was taught could be put in a implementation file for a template class were template specialization functions ie: template<> Class<Type>::function_name() . 我教过的唯一函数可以放在模板类的实现文件中,模板专用函数即: template<> Class<Type>::function_name()

Why would this book to suggest to include the .cpp at the end of the .h? 为什么这本书建议在.h结束时包含.cpp? is this just a way of separating the implementations into different files while getting them to compile with the header? 这只是一种将实现分离到不同文件,同时让它们用标题编译的方法吗? and if so, where would you put real specializations - I'm guessing they couldn't go in the .cpp included by the header. 如果是这样,你会在哪里放置真正的专业 - 我猜他们不能进入标题所包含的.cpp。

Most likely the author prefers having declaration and definition in different files, I can guess this is because it makes it easier to jump between declarations and definitions. 很可能作者更喜欢在不同的文件中使用声明和定义,我猜这是因为它可以更容易地在声明和定义之间跳转。

But having "cpp" file extension is a bit confusing. 但是“cpp”文件扩展名有点令人困惑。 Usually those files are called "ipp" for "Inline C++". 通常这些文件被称为“内联C ++”的“ipp”。

This is probably old parlance, and I think your analysis of separation of implementation and declaration is correct. 这可能是古老的说法,我认为您对实施和声明分离的分析是正确的。 Back when this particular book was written, the author probably thought of a cpp file as the file where definitions resided, and h files as the files where declarations resided. 回到编写这本特定的书时,作者可能认为cpp文件是定义所在的文件,h文件是声明所在的文件。 Putting real explicit specializations into the former file would, naturally, be lethal (or at least useless) to the linker because of repeated definitions. 由于重复的定义,将真正的显式特化放入前一个文件中对链接器来说自然是致命的(或至少是无用的)。 Nowadays, I'd avoid naming the definitions file .cpp. 如今,我会避免命名定义文件.cpp。

First, there's no rule that you have to have the implementations of the templates in your .h (or .hpp , or .hh ) file; 首先,没有规则必须在.h (或.hpp.hh )文件中具有模板的实现; in fact, for anything but the simplest templates, I would recommend against doing so. 事实上,除了最简单的模板之外,我建议不要这样做。 You do have to include the implementation, regardless of the file. 无论文件如何,您都必须包含实现。 What the author probably had in mind was to put the implementation in a .cpp file, and include that. 作者可能想到的是将实现放在.cpp文件中,并包含它。 I'd recommend finding a different name, however, since most people (and some IDE's) will suppose that you should compile all .cpp files. 但是,我建议找一个不同的名字,因为大多数人(以及一些IDE)会认为你应该编译所有的.cpp文件。 A common convention where .cc and .hh are the usual extensions for sources and headers is .tcc for template implementations; 一个常见的约定,其中.cc.hh是源和头文件的通常扩展名,是.tcc用于模板实现; in a Windows world (where .cpp is almost universal), I'd recommend something like .tpp . 在Windows世界中( .cpp几乎是通用的),我推荐像.tpp这样的.tpp

Note that the earliest implementations of templates required the implementation to be in a .cpp . 请注意,最早的模板实现要求实现在.cpp These implementations didn't require (or allow) it to be included, however; 但是,这些实现不需要(或允许)包含它; the compiler searched for the .cpp (or .cc ) corresponding to the .hpp (or .hh ) file in which the template class definition or function declaration appeared, and generated a dummy source file which included it (and anything else which was necessary). 编译器搜索与模板类定义或函数声明出现的.hpp (或.hh )文件相对应的.cpp (或.cc ),并生成包含它的虚拟源文件(以及其他任何必要的文件) )。

As far as I know you have to do full function implementations in your .h file for any template class member functions - it's due to the way the template compiler works. 据我所知,您必须在.h文件中为任何模板类成员函数执行完整的函数实现 - 这是由于模板编译器的工作方式。

May be the book assumed you have the full function implementations in your .cpp file. 可能是本书假设您在.cpp文件中具有完整的功能实现。

If full specializations are in cpp file, they should not be included in header file. 如果完全特化是在cpp文件中,则它们不应包含在头文件中。 Most likely it wont compile if you did so. 如果你这样做,很可能它不会编译。 Because compiler will see multiple definitions of the same function as header file will generally be included in multiple source files. 因为编译器会看到与头文件相同功能的多个定义通常会包含在多个源文件中。

If you want to use a template, the entire template definition has to be visible in the TU that instantiates the template. 如果要使用模板,则必须在实例化模板的TU中显示整个模板定义。 So you could simply put full definitions in the header file. 因此,您可以简单地将完整定义放在头文件中。

It's only if you really want to keep the class definition and class member function bodies separate , then you might be tempted to put those function definitions into a separate file. 只有当你真的想要将类定义和类成员函数体分开时 ,才可能将这些函数定义放在一个单独的文件中。 However, the above rule still applies, so you'll have to include the separate file together with the header in order for anyone to be able to use the template. 但是,上述规则仍然适用,因此您必须将单独的文件与标题一起包含在内,以便任何人都能够使用该模板。

It's a matter of taste. 这是一个品味问题。 If the member function bodies are short, you might as well define them inline. 如果成员函数体很短,您也可以将它们内联定义。 Also bear in mind that functions that are defined inside the class definition are implicitly declared inline , while you'd have to specify that explicitly if you write the bodies separately. 还要记住,在类定义中定义的函数是内inline隐式声明的,而如果单独编写inline ,则必须明确指定。

A partially specialised templated is still a template, so the same rules apply. 部分专门化的模板仍然是模板,因此适用相同的规则。

A fully specialized class template is just an ordinary class, so you treat is as such: 一个完全专业化的类模板只是一个普通的类,所以你这样对待:

// FooInt.hpp
template <typename> class Foo;
template <> class Foo<int>
{
  // your class here
};

One thing you may have had in mind is "explicit template instantiation", a la template class Foo<int>; 您可能想到的一件事是“显式模板实例化”,la template class Foo<int>; . That's something else; 那是另一回事; leave a comment if you're interested in using that. 如果您对使用它感兴趣,请发表评论。

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

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