TITLE: typedefs versus defines (Newsgroups: comp.lang.c++, 17 Aug 99) NUVO: Rich Nuvo > Though this answer is informative, as is typical of Siemel's posts, > I can't imagine that a question about "typedef vs. #define" is asking > about macro functions. I could be mistaken, but I would assume the > OP probably meant typedef vs. using a #define in its role as a typedef, > as in something like: > > #define String wstring > > versus: > > typedef wstring String NARAN: Siemel B. Naran I gave everything I knew about macros. Forgot that you were asking about typedefs in particular. Some elements still hold. 1. Macros don't respect scope, but typedefs do. 2. Typedefs can be used for traits, but macros can't. Consider std::deque template class deque { ... typedef T value_type; ... }; One can now write this function template typename Container::value_type sumsin(const Container& c) { typedef typename Container::value_type Value; typedef typename Container::const_iterator Iter; const Iter end=c.end(); Value out=Value(); for (Iter i=c.begin(); i!=end; ++i) out+=std::sin(*i); return out; } 3. Your compiler can use typedefs in its error messages, but it can't use macros. The result is that you get shorter, easier error messages. Most compilers don't do this, but I've seen a few recommendations that they do start to do this. 4. Your debugger can use typedefs in its debugging code, let you access a class by the typedef, and so on. It's been over two years since I used debuggers, and I don't know whether debuggers actually do this. My guess is that they don't do this as yet. 5. You can define unusual typedefs, like a pointer to a function or array. typedef int (X::*Fptr)(int,int) const; typedef int Array[10]; Be aware that "const Fptr fptr=&X::calc" makes the pointer const. That is, "int (X::*const fptr)(int,int) const=&X::calc" -- ie, you can't make 'ftpr' point to another function. Similarly, in the code typedef typename Container::const_iterator Iter; const Iter end=c.end(); if Iter is "double const *", then the second line is equivalent to double const *const end=c.end(); // 'end' can't be reseated // ^^^^^ result of "const Iter"