Template classes and include guards in C++
Asked Answered
F

4

9

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

Fye answered 8/3, 2010 at 12:28 Comment(0)
H
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.

Heterosexual answered 8/3, 2010 at 12:32 Comment(3)
Do add include guards always, get used to it, as it is a GOOD practice.Foil
If you do any development on win32, include #pragma once whenever using include guards.Valvule
+1 for stating that instantiations are done using the internal data structure built at the time of first sweepIona
F
13

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.

Fetishism answered 8/3, 2010 at 12:35 Comment(2)
Strictly that's a template function rather than a template class, but the principle is the same - you get a multiple definition error if you declare the same class twice, template or not.Onetoone
See my answer here - #22595515Indemnification
H
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.

Heterosexual answered 8/3, 2010 at 12:32 Comment(3)
Do add include guards always, get used to it, as it is a GOOD practice.Foil
If you do any development on win32, include #pragma once whenever using include guards.Valvule
+1 for stating that instantiations are done using the internal data structure built at the time of first sweepIona
Y
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.

Yalonda answered 8/3, 2010 at 13:17 Comment(0)
U
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.

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.

Undeceive answered 8/3, 2010 at 13:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.