简体   繁体   English

带模板的C ++标头顺序

[英]C++ header order with template

I've been new to C++, and is now going through templates and have encountered this question. 我是C ++的新手,现在正在遍历模板,并遇到了这个问题。

When the template type requires another class, will there be a specific requirement of the header include order? 当模板类型需要另一个类时,是否对标头包含顺序有特定要求?

vector<string> stringVector;

Like this: should we include string prior to vector ? 这样:我们应该在vector之前包含string吗?

I read this ( C++ Header order ) and it's saying that the header files should be included in the class-requirement order. 我读到了这个( C ++标题顺序 ),这是说标题文件应该包含在类要求顺序中。

However, as this ( Template Compilation ) indicates, or if it is my misunderstanding, "the compiler generates the code for the specific types given in the template class instantiation" , and I think this means that when we are instantiating the stringVector , the compiler already included string header, so there should not be a " vector here is string required" relationship. 但是,正如这( Template Compilation )表明的那样,或者如果是我的误解, “编译器会为模板类实例化中给出的特定类型生成代码” ,我认为这意味着当我们实例化stringVector ,编译器已经包含了string标题,因此不应存在“ vector在这里是string必需”的关系。

So, which interpretation is right, and which part of my interpretation is right or wrong? 那么,哪种解释是正确的,而我的解释的哪一部分是对还是错? Thanks. 谢谢。

Whenever you use a template in c++, the used template type has to be known as a complete type, that requires you to include the string class when you want to use a vector of string. 每当您在c ++中使用模板时,使用的模板类型都必须称为完整类型,当您要使用字符串向量时,要求您包括字符串类。 The includes are nothing more than copying and pasting the code in the included file to where your include is placed. 包含项只不过是将包含文件中的代码复制并粘贴到放置包含项的位置。

1> #include <string>
2> #include <vector>
3>  
4> class Foo {
5> private:
6>     vector<string> bar;    
7> }

When the line 6 is compiled, the compiler has to know both types as complete type (string because its a template, vector because its not a pointer). 编译第6行时,编译器必须将这两种类型都称为完整类型(字符串是因为是模板,而矢量是因为不是指针)。 The includes are placed over the class so the compiler knows both types when he wants to compile line 6. It doesn't matter which order you included them. 包含放在类上,这样,当编译器要编译第6行时,编译器就可以同时知道这两种类型。

You basically have to include all the dependecies before using them. 使用它们之前,您基本上必须包括所有依赖项。 So it does not matter in your example, if string or vector is included first. 因此,在您的示例中,是否首先包含字符串或向量并不重要。 They just have to be included both before using them. 使用它们之前,只需将它们都包括在内。

The order does matter if the header files are dependend from each other. 头文件是否相互依赖,顺序确实很重要。 Lets say there is a header file ah and bh, where bh is dependend from ah THEN ah has to be included first. 可以说有一个头文件ah和bh,其中bh是ah的从属,然后必须首先包括ah。 But if someone has to do it that way, the program is not written in a clean way. 但是,如果有人必须这样做,则该程序不是以干净的方式编写的。 All dependecies of a header file should be included in a header file using include guards and not somewhere else. 头文件的所有依赖关系都应使用包含保护(而不是其他地方)包含在头文件中。

This is as far as i understand it. 据我了解。

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

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