Thread: Typedef advantages?

  1. #1
    Registered User
    Join Date
    Sep 2006

    Typedef advantages?

    Just wondering if anyone has solid advice on when and how to use typedef.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    United States
    Use typedef to give a long and difficult type an alias.
    typedef std::map< int, vector<string> > StrIntMap;
    typedef int(*compare)(void *a, void *b) CompFn;
    It is better to assume that there is no advantage at all, and only use typedef if writing out a variable's type is very fugly looking or especially error prone.
    Last edited by whiteflags; 10-19-2006 at 11:14 AM.

  3. #3
    Registered User
    Join Date
    Jan 2005
    >> writing out a variable's type is very fugly looking or especially error prone.
    That is the advantage.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Use it when you gain a lot more in terms of clarity than you lose with the loss of detail in the code.

    Function pointers are notoriously hard to get right, and exceedingly verbose if you have several parameters. This is usually a good idea to typedef these.

    For a given T, using a typedef for T*
    Eg. typedef int *intp;
    You haven't saved any typing, you may as well type a * before something as type a 'p' after it. But you have made the code a hell of a lot harder to trace through since the number of *'s you use on the usage no longer agrees with the number of *'s on the declaration.
    This is a bad use of typedef.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Excellent! Thank you... I had never really understood this. I already have several uses for this.

  6. #6
    Registered User
    Join Date
    May 2003
    If you write any class which you want to be usable like an STL container, you'll need to typedef things like iterators and the like.

    Also, citizen, I think your function pointer typedef should just be:

    typedef int(*CompFn)(void *a, void *b);
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  3. Please STICKY this- vital to MSVC 6 dev - BASETSD.h
    By VirtualAce in forum Game Programming
    Replies: 11
    Last Post: 03-15-2005, 09:22 AM
  4. build errors migrated from dx9b to dx9c sdk
    By reanimated in forum Game Programming
    Replies: 4
    Last Post: 12-17-2004, 07:35 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM