views:

271

answers:

5

I have a typedef

 typedef unsigned int my_type;

used in a file. I would like to make it visible across all my files, without putting it in a header file included by everything. I don't want to go the header file route because as it stands this will be the only declaration in the header file (and it seems unnecessary to add a file just for this).

Is there a way to do this?

If instead I had:

typedef X my_type;

where X was a class, would I need to include X.h everywhere and have the typedef at the end of X.h ?

+4  A: 

No way around this as far as I can see. Why don't you make a globals.h header file with just the declarations you want everywhere and include that?

Don't be tempted to bury your typedef somewhere and hope that 'since everything else hangs off the header' that it'll be as good as adding a global header - it is extremely bad practice to have headers that are not self contained.

Also, to prevent cluttering up the global namespace create your own:

namespace MyTypes
{
    typedef A B;
    const unsigned int g_nMyGlobalType = 10;
    // etc.
}

That way you can use your globals in a nice uncluttered way:

MyTypes::B myVar; // etc
Konrad
The global header solution is not very pretty either (from a dependency point of view), notably I would recommend NOT to use it as THE default header ;)
Matthieu M.
+1  A: 

I would use the header file route, it's not so bad.

No you wouldn't need to include X.h everywhere, just in the places where you use the typedef.

demoncodemonkey
+10  A: 

I don't want to go the header file route because as it stands this will be the only declaration in the header file (and it seems unnecessary to add a file just for this).
What's the problem with that? It seems just as unnecessary to avoid creating a file at all costs.

Is there a way to do this?
Not as far as I am aware.

would I need to include X.h everywhere and have the typedef at the end of X.h ?
No, but that's probably the best thing. The only reason you should be doing that is if X is a template, and you need templates to be in headers anyway.

Billy ONeal
another reason would be X being a forward declaration.
peterchen
@peterchen: Then X shouldn't be typedef'd, you should be using the class's name, X.
Billy ONeal
:shrug: There are very few reasons to typedef a "pure" class name anyway - but hey, it wasn't my idea!
peterchen
A: 

No, there's no way to get around needing to have the typedef in a header file that is included everywhere you need it, but no, you don't necessarily need to also include the X.h file in the second case.

If you have

typedef X my_type;

And X is a class, you could do this (in a header other than X.h):

class X;
typedef X my_type;

Which forward-declares X, and then creates a type alias for it. Then, have X.h include this file.

Then, if you only need the incomplete typename my_type, you can include the very short typedef header file, and you only need to include X.h when you would have needed a complete definition for the X class anyway.

Tyler McHenry
A: 

Declaring something (eg. a class, typedef, function) in a translation unit (eg. cpp file) is actually pretty much a standard way of saying "this is only used in this file", because it's impossible to reference it from other files. Translation units have no idea of each other's existance, other than the declarations (eg. prototypes) shared by .h files - which say nothing of implementation. It's the linker that takes all the implementations and pastes them together in to a binary.

If you want it to be visible outside the file - put it in a .h file and include it. As other answers have suggested, a common or global .h file might be useful. Some compilers which make use of precompiled headers make this easy - put it in the precompiled header.

AshleysBrain