Reputation: 38206
Say i have three files
//first.h
typedef typename std::map<Vertex_t*,Vd_t> MapVertexVd_t;
//the graph class and ...
//other useful things related to a graph class
//second.h
#include "first.h"
class analyzeGraph {
MapVertexVd_t mapVertexVd;
public:
void fillMap();
};
//second.cpp
#include "second.h"
void analyzeGraph::fillMap()
{
//
}
Now the question is should i place:
typedef typename std::map<Vertex_t*,Vd_t> MapVertexVd_t;
in the files second.h and second.cpp for clarity sake. I feel like i'll get confused after some time since i have way too many typedef and typename all over the place in my code.
Upvotes: 3
Views: 2254
Reputation: 754450
In C89 or C99 (or pre-standard C), a repeat of a typedef at the same scope is a compilation error, nuisance though it is. However, C++ is more civilized and allows 'benign redefinition' (see §7.1.3, ¶2, according to Charles Bailey). Despite this licence, one of the Agile (or Pragmatic) Programming mottos is "DRY: Don't repeat yourself", which in this context means you should not write the typedef
in more than one header. You could consider creating a header zeroth.h
that is included by each of the other headers (first.h
, second.h
); it should define or forward declare the type Vertex_t
; it must either define or include the declaration of Vd_t
(as well as specifying the type MapVertexVd_t
). It should also include the relevant standard header (<map>
) so that the zeroth.h
header is self-contained and can be used without further ado by any code that needs it.
Note that C11 also allows benign redefinition of types:
¶3 If an identifier has no linkage, there shall be no more than one declaration of the identifier (in a declarator or type specifier) with the same scope and in the same name space, except that:
a typedef name may be redefined to denote the same type as it currently does, provided that type is not a variably modified type;
tags may be redeclared as specified in 6.7.2.3.
By contrast, the corresponding section of C99 says:
§3 If an identifier has no linkage, there shall be no more than one declaration of the identifier (in a declarator or type specifier) with the same scope and in the same name space, except for tags as specified in 6.7.2.3.
Upvotes: 3
Reputation: 208406
Avoid repeating the typedef
in different translation units, if you later change one of them you will end up with a maintenance nightmare having to chase all places where you defined the same typedef.
My simple rule is define it where it makes sense. In many cases that will be together with some of the related types (maybe with the definition of Vertex_t
) and use sensible names for the typedef that indicate what it is. Only if you don't know what Vertex_t
or Vd_t
are you would need to go back and check what MapVertexVd_t
is, and then having a local typedef will not help either (I don't know what Vertex_t
or Vd_t
represent, but choose sensible names that will ring the bell as to what the type is and what operations you can perform on it).
Upvotes: 2
Reputation: 25543
I find that depending on the circumstances these work out well:
Upvotes: 5
Reputation: 856
if MapVertexVd_t is used only inside of the analyzeGraph it is better to place the typedef inside of the analyzeGraph class.
Upvotes: 3
Reputation: 20272
No, make sure that the declarations are not duplicated.
The way you made it now is actually pretty good - you have a separate H file with the typedef
, and it is included everywhere the type is used. That's how it is supposed to be.
The problem with duplication is that some compilers might not like it even if they are identical. If at some point you have to change the typedef
- then you're in a total mess. That is why you should avoid duplicating.
Upvotes: 3