Jump to content


What are "typedef"s in C++ really used for or necessary for?


22 replies to this topic

#21 .oisyn

    DevMaster Staff

  • Moderators
  • 1842 posts

Posted 06 January 2012 - 11:21 PM

View PostTheNut, on 06 January 2012 - 05:52 PM, said:

The C standard should have added specific primitive type sizes since day one, like what .NET did.
But that's comparing apples and oranges. .NET code only runs on a specific welldefined machine - the .Net virtual machine. C, on the other hand, is designed to be compiled for any thinkable platform. It's perfectly viable for a C implementation to have an int type that is, say, 19 bits.

And in that respect, it makes no sense for you to define your loop counter to be of a size-specific type, as long as it's large enough to hold all the possible iteration values. You usually just want the type that is most efficient. And an int32 on a 64 bit platform might not necessarily be it. Hard-defined sized ints only make sense for storage (eg, in structs and classes), not for local function variables.
C++ addict
-
Currently working on: the 3D engine for Tomb Raider.

#22 Xcrypt

    New Member

  • Members
  • PipPipPip
  • 144 posts
  • LocationBelgium

Posted 07 January 2012 - 03:11 AM

I think sometimes typedefs are necessary. I've been in a situation like this before

template<class edge_type, class node_type>
class Graph
{
public:
typedef edge_type EdgeType;
typedef node_type NodeType;
[...]
};

template<class graph_type, class heuristic>
class GraphSearchAlgorithm
{
//this needs access to the edge_type and node_type
}

I don't think they are avoidable in a situation like this. If they are, correct me.

Also, I hate overusing typedefs myself, but sometimes it can be handy to use them when you get to deal with annoying dependant names like A::B::C: :D::E::F::G::H::I<J::K::L<M::N: :o, P::Q::R< S::T, U::V> >::W::X>::Y::Z ;D

You might think it's good to see where the types come from for example, without having to scroll over the identifier. But sometimes it's really not worth sacrificing the speed and code clarity of using typedefs, especially when dealing with ultra long names. This doesn't mean that you have to make your typedefs global, it can be good to do so within a class or function too, and it will hide outside of those functions/classes

They're also way better than defines for debugging.

#23 Stainless

    Member

  • Members
  • PipPipPipPip
  • 582 posts
  • LocationSouthampton

Posted 07 January 2012 - 09:57 AM

#defines are evil

I lost three hours on Thursday trying to figure out why the compiler was throwing out an error about a loc does nothing.

It was in a bit of code that specified inline functions on a namespace rather than in a class. So at first I thought it was an issue with our compiler.

Eventually I forced the compiler to save the results of the preprocessing and found that I had left a semicolon dangling on a #define

After hitting my head against the wall a few times I fixed the problem in 2 seconds flat.

Knowing the real size of variables was very important to me, with the new compiler it's not so much of an issue, but with the last compiler I spent most of my time sorting out bugs like this.....

typedef struct Buggy
{
char a;
int b;
char c;
union
{
char * pa;
int * pb;
}u;
}buggy_t;

If char is a KDuint8 then I will get an alignment error every time I access pb.

Typedefs are really useful when you move away from main stream coding and start working on devices other than PC's





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users