what is the advantage of typedefs over macros?
typedef
Collapse
This topic is closed.
X
X
-
bob@coolgroups.comTags: None -
John Carson
Re: typedef
<bob@coolgroups .com> wrote in message
news:1136452271 .928086.292980@ g43g2000cwa.goo glegroups.com[color=blue]
> what is the advantage of typedefs over macros?[/color]
1. they respect scoping,
2. some C++ constructs are only possible with typedefs, e.g., conversion
operators that return function pointers (or at least I have never figured
out how to write them without typedefs),
3. a nicer syntax in some contexts, e.g., function pointers become easy to
use when you have a typedef, whereas macros don't offer nearly as consistent
syntax.
--
John Carson
-
Michiel.Salters@tomtom.com
Re: typedef
John Carson wrote:[color=blue]
> <bob@coolgroups .com> wrote in message
> news:1136452271 .928086.292980@ g43g2000cwa.goo glegroups.com[color=green]
> > what is the advantage of typedefs over macros?[/color]
>
> 1. they respect scoping,
> 2. some C++ constructs are only possible with typedefs, e.g., conversion
> operators that return function pointers (or at least I have never figured
> out how to write them without typedefs),
> 3. a nicer syntax in some contexts, e.g., function pointers become easy to
> use when you have a typedef, whereas macros don't offer nearly as consistent
> syntax.[/color]
In addition to 1, they respect a lot more C++ features. E.g. the
template < >
brackets are parsed in a typedef declaration, by the (real) compiler.
They're
not parsed by the preprocessor (which is responsible for macros).
Argument 1 is very important, it's not just lexical scope. E.g. in
template <T> class Foo {
typedef std::vector<T> Bar;
};
there's no way to define a similar macro Bar, because the parameter T
is
only visible in the lexical scope. Yet Bar becomes visible in the name
lookup
scope of every Foo instantiation.
HTH,
Michiel Salters
Comment
-
Randy
Re: typedef
In addition to all these, many debuggers utilize typedef identifiers,
so
it makes it easier to debug your code.
--RY
Comment
Comment