Why are type aliases useful?

Sep 30, 2014 at 7:13pm
From what I understand, using the keyword typedef is basically giving a valid type a different name. Such as...

 
typedef int integer;


So now the word integer can be used the same way int can.

but my question is.. how is this useful? wouldn't using the original valid type be more quicker and simple than using aliases?
Sep 30, 2014 at 8:12pm
In that case, yes. What about something like
typedef std::map<std::string, std::vector<int> > NameToNumbersMap;
Using NameToNumbersMap as the typename is far less cumbersome than typing out the actual type.
Last edited on Sep 30, 2014 at 8:13pm
Sep 30, 2014 at 8:20pm
That is an overly simplified example.

See more complex cases in http://en.cppreference.com/w/cpp/language/typedef

Some reasons:
http://www.gotw.ca/gotw/046.htm
Sep 30, 2014 at 10:34pm
1) typedef are important for updating a software program for example

typedef float FLOAT

if you have a bunch of dataTypes of FLOAT you can change all their dataTypes by just changing the typeDef form

typedef int FLOAT

This is just for demonstrative purposes..

2) You can give a variable a more explicit name when working on different system schemes and stuff like that

Last edited on Sep 30, 2014 at 10:37pm
Topic archived. No new replies allowed.