San said:
hi there,
I am new to c++ and tryig to learn the basics of the c++ concepts.
While I was reading the templates, I realize that the templates are a
syntax that the compilar expands pased upon the type specified. This is
much similar like a macro expansion in C.
can anyone please explain advantages of one over the other ?
Thanks in advance -
San
A macro invocation translates into a arbitrary sequence of token.
The parameters passed to a macro are also arbitrary sequences
of tokens. Translation of macros can be thought of as a
phase in compilation that takes place between tokenization
and parsing. This is why macros can't be scoped by classes,
namespaces, etc., because those haven't been parsed yet.
The simplicity and flexibility of a macro is exactly what's
called for some purposes. But when someone sees a macro
in some code they're trying to understand, they have to consider
alot of possibilities as to what it might be doing. So avoiding the
use of macros will make you more popular with people who
have to understand your code. Said people as thus less
likely to tell your boss that you suck.
Template translation is part of parsing, and can't be thought
of as a separate phase. When the parser comes to a
template invocation, it wormholes up to file scope and
expands the template (with an implicit "using" declaration
of the partially-defined namespaces containing the
invocation). It's this wormholing behavior that gives
templates an importance that goes far beyond just
being well-behaved macros.