3

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.

1
  • 3
    typedefs are supposed to make the code easier to understand, not harder. If they confuse you, perhaps you shouldn't use that many?
    – Bo Persson
    Jul 13, 2011 at 6:59

5 Answers 5

5

I find that depending on the circumstances these work out well:

  • For typedefs used across a lot of files, put them in a shared header file (probably with forward declarations, etc.)
  • For typedefs used only by a few functions or methods (in the declaration), place them immediately before the first relevant declaration, and make sure that the functions are declared together.
  • For typedefs used for data in a class or function, place them at the start of the relevant scope so that people reading the code see them first. (And for classes make the visibility of typedefs as low as possible -- private typedefs should remain private.)
0
3

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.

2
  • then please tell me what is the best way to avoid getting confused in future.
    – A. K.
    Jul 13, 2011 at 6:55
  • @Aditya - what is confusing? It actually looks very well structured. Generally I would try to bring the related typedefs together in one header, and include it where they are used. For each class and all the typedef's related to it - have a separate header file which encapsulates them all, and that's it. Make sure to have the include guards though to avoid duplicates where not intended.
    – littleadv
    Jul 13, 2011 at 6:58
3

if MapVertexVd_t is used only inside of the analyzeGraph it is better to place the typedef inside of the analyzeGraph class.

3

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:

ISO/IEC 9899:2011 §6.7 Declarations

¶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:

ISO/IEC 9899:1999 §6.7 Declarations

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

4
  • 1
    No, it's perfectly legal to redeclare a typedef in C++: 7.1.3/2. "In a given non-class scope, a typedef specifier can be used to redefine the name of any type declared in that scope to refer to the type to which it already refers."
    – CB Bailey
    Jul 13, 2011 at 7:01
  • @Charles: damn - I knew it was a mistake not having a copy of the C++ standard on this computer as well as my other one! Jul 13, 2011 at 7:02
  • 2
    +1 from me: Even if it's allowed, it's not (usually) a good idea.
    – CB Bailey
    Jul 13, 2011 at 7:14
  • your idea and particularly the file name zeroth.h is pretty interesting.
    – A. K.
    Jul 13, 2011 at 7:15
2

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

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.