包括.cpp在模板头文件的末端。

时间:2021-12-26 15:06:48

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 i.e.: template<> Class<Type>::function_name().

我所教授的唯一功能可以放在模板类的实现文件中,即模板类的专门化函数。:模板类 <类型> < >::function_name()。

Why would this book to suggest to include the .cpp at the end of the .h? 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.

为什么这本书会建议在。h结尾加上。cpp呢?这仅仅是一种将实现分离到不同文件的方法吗?如果是这样的话,你会把真正的专门化放在哪里呢?我猜它们不可能出现在header中包含的。cpp中。

5 个解决方案

#1


10  

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. Usually those files are called "ipp" for "Inline C++".

但是有“cpp”文件的扩展有点让人困惑。通常这些文件被称为“ipp”,表示“内联c++”。

#2


3  

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. 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文件是定义所在的文件,h文件是声明所在的文件。将真正明确的专门化放到前一个文件中,自然会对链接器造成致命的(或至少是无用的),因为有重复的定义。现在,我避免命名定义文件.cpp。

#3


1  

First, there's no rule that you have to have the implementations of the templates in your .h (or .hpp, or .hh) file; 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. I'd recommend finding a different name, however, since most people (and some IDE's) will suppose that you should compile all .cpp files. A common convention where .cc and .hh are the usual extensions for sources and headers is .tcc for template implementations; in a Windows world (where .cpp is almost universal), I'd recommend something like .tpp.

首先,没有规则规定必须在.h(或.hpp或.hh)文件中包含模板的实现;事实上,除了最简单的模板之外,我建议不要这样做。无论文件是什么,都必须包含实现。作者可能想到的是将实现放在.cpp文件中,并包含它。不过,我建议您找一个不同的名称,因为大多数人(以及一些IDE)会认为应该编译所有的.cpp文件。一个常见的约定是,.cc和.hh是源的常用扩展,而模板实现的.tcc是.tcc;在Windows世界中(.cpp几乎是通用的),我推荐一些类似于.tpp的东西。

Note that the earliest implementations of templates required the implementation to be in a .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).

注意,模板的早期实现要求实现位于.cpp中。然而,这些实现并不要求(或允许)包含它;编译器搜索与出现模板类定义或函数声明的.hpp(或.cc)文件对应的.cpp(或.hpp),并生成包含该文件的虚拟源文件(以及任何必要的内容)。

#4


0  

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. 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.

如果全部专门化都在cpp文件中,它们不应该包含在头文件中。如果您这样做,它很可能不会编译。因为编译器会看到与头文件相同的函数的多个定义通常包含在多个源文件中。

#5


0  

If you want to use a template, the entire template definition has to be visible in the TU that instantiates the template. So you could simply put full definitions in the header file.

如果想使用模板,整个模板定义必须在实例化模板的TU中可见。所以你可以把完整的定义放在头文件中。

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.

只有当您真的想要保持类定义和类成员函数体分离时,您才可能尝试将这些函数定义放到一个单独的文件中。但是,上面的规则仍然适用,所以您必须将单独的文件和header一起包含在一起,以便任何人都能够使用这个模板。

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.

这是品味的问题。如果成员函数体很短,您也可以内联地定义它们。还要记住,在类定义中定义的函数是隐式声明的内联的,而如果分别编写主体,则必须显式地指定这一点。

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>;. That's something else; leave a comment if you're interested in using that.

您可能想到了“显式模板实例化”,这是一个la模板类Foo ;这是其他东西;如果你有兴趣的话,请留下评论。

#1


10  

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. Usually those files are called "ipp" for "Inline C++".

但是有“cpp”文件的扩展有点让人困惑。通常这些文件被称为“ipp”,表示“内联c++”。

#2


3  

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. 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文件是定义所在的文件,h文件是声明所在的文件。将真正明确的专门化放到前一个文件中,自然会对链接器造成致命的(或至少是无用的),因为有重复的定义。现在,我避免命名定义文件.cpp。

#3


1  

First, there's no rule that you have to have the implementations of the templates in your .h (or .hpp, or .hh) file; 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. I'd recommend finding a different name, however, since most people (and some IDE's) will suppose that you should compile all .cpp files. A common convention where .cc and .hh are the usual extensions for sources and headers is .tcc for template implementations; in a Windows world (where .cpp is almost universal), I'd recommend something like .tpp.

首先,没有规则规定必须在.h(或.hpp或.hh)文件中包含模板的实现;事实上,除了最简单的模板之外,我建议不要这样做。无论文件是什么,都必须包含实现。作者可能想到的是将实现放在.cpp文件中,并包含它。不过,我建议您找一个不同的名称,因为大多数人(以及一些IDE)会认为应该编译所有的.cpp文件。一个常见的约定是,.cc和.hh是源的常用扩展,而模板实现的.tcc是.tcc;在Windows世界中(.cpp几乎是通用的),我推荐一些类似于.tpp的东西。

Note that the earliest implementations of templates required the implementation to be in a .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).

注意,模板的早期实现要求实现位于.cpp中。然而,这些实现并不要求(或允许)包含它;编译器搜索与出现模板类定义或函数声明的.hpp(或.cc)文件对应的.cpp(或.hpp),并生成包含该文件的虚拟源文件(以及任何必要的内容)。

#4


0  

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. 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.

如果全部专门化都在cpp文件中,它们不应该包含在头文件中。如果您这样做,它很可能不会编译。因为编译器会看到与头文件相同的函数的多个定义通常包含在多个源文件中。

#5


0  

If you want to use a template, the entire template definition has to be visible in the TU that instantiates the template. So you could simply put full definitions in the header file.

如果想使用模板,整个模板定义必须在实例化模板的TU中可见。所以你可以把完整的定义放在头文件中。

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.

只有当您真的想要保持类定义和类成员函数体分离时,您才可能尝试将这些函数定义放到一个单独的文件中。但是,上面的规则仍然适用,所以您必须将单独的文件和header一起包含在一起,以便任何人都能够使用这个模板。

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.

这是品味的问题。如果成员函数体很短,您也可以内联地定义它们。还要记住,在类定义中定义的函数是隐式声明的内联的,而如果分别编写主体,则必须显式地指定这一点。

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>;. That's something else; leave a comment if you're interested in using that.

您可能想到了“显式模板实例化”,这是一个la模板类Foo ;这是其他东西;如果你有兴趣的话,请留下评论。