Syntax Means Little With Typedef

Typedefs turn ugly syntax into something meaningful. (namespace declarations can help too). before:

 plugware::thread_pool<>::instance().queue_request(timer_pool::instance().create<plugware::work_timer>());

becomes:

 typedef plugware::thread_pool<> thread_pool;
 typedef plugware::work_timer    work;
 thread_pool::instance().queue_request(timer_pool::instance().create<work>());

this may not be the best example, but it hides what some complain of as ugly CeePlusPlus template syntax. It can also be used to hide PatternLanguageInCode.

An alternative to typedef is to write a wrapper class for the template. This is my preference because it hides the template syntax from the user of the template/class, while keeping the syntax exposed to the developer of the wrapping class. If one uses inline methods, there is no performance or memory size loss compared to the typedef implementation, and the class tends to provide a nice place to hold all of the definitions. This may be more of a personal preference, I just thought I would suggest it. --WayneMack

A very common example of where typedef is a GoodThing (something along these lines):
 std::map<std::string, std::vector<std::string> > mymap;
 for(std::map<std::string, std::vector<std::string> >::iterator it = mymap.begin(); it != mymap.end(); ++it) { }
(My eyes!)

 typedef std::map<std::string, std::vector<std::string> > mymap_t;
 mymap_t mymap;
 for(mymap_t::iterator it = mymap.begin(); it != mymap.end(); ++it) { }
That's almost pretty! One could (should?), of course, avoid the former case by using std::for_each, but typedef also means that one can change the actual type in only one place. (Realize that deque is in fact better for this than vector, or some other similar change of plans.)


So... I see the 'With Typedef' part, but what does all the above have to do with 'Syntax Meaning Little'?


CategoryCpp

EditText of this page (last edited August 11, 2007) or FindPage with title or text search