模板类,并包含c++中的警卫

时间:2021-11-24 17:05:20

Is it wise to have include guards around template classes?

在模板类周围包含警卫是明智的吗?

Aren't template classes supposed to be reparsed each time you reference them with a different implementation?

难道不应该在每次使用不同的实现引用模板类时进行修复吗?

N.B In Visual C++ 2008 I get no errors combining the two...

N。在Visual c++ 2008中,我将两者结合在一起不会出现错误。

4 个解决方案

#1


8  

Templates definitions are supposed to be parsed once (and things like two phases name lookup are here so that as much errors as possible can be given immediately without having an instantiation). Instantiations are done using the internal data structure built at that time.

模板定义应该被解析一次(这里有两个阶段名称查找,这样就可以在没有实例化的情况下立即给出尽可能多的错误)。实例化是使用当时构建的内部数据结构完成的。

Templates definitions are usually (i.e. if you aren't using export or doing something special) in header files which should have their include guard. Adding one for template definition is useless but not harmful.

模板定义通常(例如,如果您不使用导出或做一些特殊的事情)在头文件中,头文件应该具有包含保护。为模板定义添加一个模板是无用的,但不是有害的。

#2


12  

You need include guards. Consider this code:

你需要包括警卫。考虑这段代码:

// this is t.h
template <typename T>
void f( T t ) {
}

// this is t.cpp
#include "t.h"
#include "t.h"

int main() {
    f( 1 );
}

This gives the error:

这就给了错误:

t.h:2: error: redefinition of 'template<class T> void f(T)'
t.h:2: error: 'template<class T> void f(T)' previously declared here

Also, the headers that contain templates routinely also contain non-template code.

同样,包含模板的头通常也包含非模板代码。

#3


2  

Short answer: Every unit you plan to include more than once with any definitions should have a header guard. That is with or without templates.

简短的回答:您计划在任何定义中包含多次的每个单元都应该有一个头保护。不管有没有模板。

#4


2  

To answer your first question: Yes, it is wise, and mandatory, to have include guards around template classes. Or more strictly surrounding the entire contents of every header file.

要回答您的第一个问题:是的,在模板类周围包含警卫是明智的,也是必须的。或者更严格地围绕每个头文件的整个内容。

This is the way to obey the One Definition Rule when you have stuff in header files, so that its shared around and still safe. There may be other header files that include yours. When the compiler compiles a module file, it may see a #include of your header file many times, but the guards kick-in on the second and subsequent times to make sure the compiler only sees the contents once.

当你在头文件中有东西时,这是遵守一个定义规则的方法,这样它就可以被共享并且仍然是安全的。可能还有包括你的头文件。当编译器编译一个模块文件时,它可能会多次看到一个#include的头文件,但是第二次和以后的时候会进行保护,以确保编译器只看到一次内容。

Its doesn't matter that the compiler reparses anything; that's its job. You just have to supply the contents once and then the compiler has seen it and can refer to it again as many times as it needs.

编译器修复任何东西都没有关系;这是它的工作。您只需提供一次内容,编译器就会看到它,并可以根据需要多次引用它。

#1


8  

Templates definitions are supposed to be parsed once (and things like two phases name lookup are here so that as much errors as possible can be given immediately without having an instantiation). Instantiations are done using the internal data structure built at that time.

模板定义应该被解析一次(这里有两个阶段名称查找,这样就可以在没有实例化的情况下立即给出尽可能多的错误)。实例化是使用当时构建的内部数据结构完成的。

Templates definitions are usually (i.e. if you aren't using export or doing something special) in header files which should have their include guard. Adding one for template definition is useless but not harmful.

模板定义通常(例如,如果您不使用导出或做一些特殊的事情)在头文件中,头文件应该具有包含保护。为模板定义添加一个模板是无用的,但不是有害的。

#2


12  

You need include guards. Consider this code:

你需要包括警卫。考虑这段代码:

// this is t.h
template <typename T>
void f( T t ) {
}

// this is t.cpp
#include "t.h"
#include "t.h"

int main() {
    f( 1 );
}

This gives the error:

这就给了错误:

t.h:2: error: redefinition of 'template<class T> void f(T)'
t.h:2: error: 'template<class T> void f(T)' previously declared here

Also, the headers that contain templates routinely also contain non-template code.

同样,包含模板的头通常也包含非模板代码。

#3


2  

Short answer: Every unit you plan to include more than once with any definitions should have a header guard. That is with or without templates.

简短的回答:您计划在任何定义中包含多次的每个单元都应该有一个头保护。不管有没有模板。

#4


2  

To answer your first question: Yes, it is wise, and mandatory, to have include guards around template classes. Or more strictly surrounding the entire contents of every header file.

要回答您的第一个问题:是的,在模板类周围包含警卫是明智的,也是必须的。或者更严格地围绕每个头文件的整个内容。

This is the way to obey the One Definition Rule when you have stuff in header files, so that its shared around and still safe. There may be other header files that include yours. When the compiler compiles a module file, it may see a #include of your header file many times, but the guards kick-in on the second and subsequent times to make sure the compiler only sees the contents once.

当你在头文件中有东西时,这是遵守一个定义规则的方法,这样它就可以被共享并且仍然是安全的。可能还有包括你的头文件。当编译器编译一个模块文件时,它可能会多次看到一个#include的头文件,但是第二次和以后的时候会进行保护,以确保编译器只看到一次内容。

Its doesn't matter that the compiler reparses anything; that's its job. You just have to supply the contents once and then the compiler has seen it and can refer to it again as many times as it needs.

编译器修复任何东西都没有关系;这是它的工作。您只需提供一次内容,编译器就会看到它,并可以根据需要多次引用它。