difference between templates and macros

S

San

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
 
R

Rickfjord

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

Hi San,

In C++ there is a major difference between a template and a macro. A
macro is merely a string that the compiler replaces with the value that
was defined. E.g.
#define STRING_TO_BE_REPLACED "ValueToReplaceWith"

A template is a way to make functions independent of data-types. This
cannot be accomplished using macros. E.g. a sorting function doesn't
have to care whether it's sorting integers or letters since the same
algorithm might apply anyway.

Hope this helped.
 
I

Ian Collins

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.
Not realy, macros are expanded by the preprocessor, the compiler doesn't
see them. They are simple test substitution.
can anyone please explain advantages of one over the other ?
The only practical advantage of macros is the ability to call a function
with __FILE__ and __LINE__ for debugging.

Macros don't have any scope, being simple text substitution. Macros
can't be stepped into with a debugger. Macros can't be specialised.
Macros aren't type safe, the list goes on.
 
F

Frederick Gotham

San posted:
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


If you want to specialise a macro function, you have to pre-define it
outside of the function in which it's used. Consider a function which
returns the length of a null-terminated array:

#include <cstddef>
using std::size_t;

#define DEF(Type)\
size_t LenNullTerm(Type const *p)\
{\
size_t i = 0;\
while(*p++) ++i;\
return i;\
}

If we want to use this function on three different types, we have to pre-
define them as follows:

DEF(char)
DEF(double)
DEF(int)

int main()
{
int array1[5] = {3,4};

char array2[5] = {'a','b','c'};

double array3[5] = {5.2,3.4,6.2};

LenNullTerm(array1);
LenNullTerm(array2);
LenNullTerm(array3);
}

With templates, there's no need to pre-define the function with "DEF".
Instead, we just have:

#include <cstddef>

template<class T>
size_t LenNullTerm(T const *p)
{
size_t i = 0;
while(*p++) ++i;
return i;
}

int main()
{
int array1[5] = {3,4};

char array2[5] = {'a','b','c'};

double array3[5] = {5.2,3.4,6.2};

LenNullTerm(array1);
LenNullTerm(array2);
LenNullTerm(array3);
}
 
M

Marcus Kwok

Ian Collins said:
Not realy, macros are expanded by the preprocessor, the compiler doesn't
see them. They are simple test substitution.

#define test text
The only practical advantage of macros is the ability to call a function
with __FILE__ and __LINE__ for debugging.

There are also the token pasting and string quoting abilities, though I
rarely if ever use those either.
Macros don't have any scope, being simple text substitution. Macros
can't be stepped into with a debugger. Macros can't be specialised.
Macros aren't type safe, the list goes on.

I agree.
 
G

Greg Comeau

#define test text


There are also the token pasting and string quoting abilities, though I
rarely if ever use those either.


I agree.

Macros do have scope, the problem (at least one of them) is that
as the preprocessor is effectively a seperate language intertwinded
with C++ (and C) it has its own view of things, so it's a different
kind of scope, etc.
 
S

San

Hello All -

Many thanks for replying to my query, it was helpful to some extent.
Macros don't have any scope, being simple text substitution. Macros
can't be stepped into with a debugger. Macros can't be specialised.
Macros aren't type safe, the list goes on.

I agree to the advantages mentioned above, but then I was thinking of
some more compelling advangates as oppose to the complexity the
templates beings in.

Specialization - as explained by Frederick , in my opinion (may be
incorrect), may not bring the compelling advantages either, as there
would be already the functionality available in the "C" standard. So,
just was looking it from the larger advantages from the C++ standards
perspective.

Once again - many thanks for all your replies.

Regards,
San
 
G

Greg Comeau

Many thanks for replying to my query, it was helpful to some extent.


I agree to the advantages mentioned above, but then I was thinking of
some more compelling advangates as oppose to the complexity the
templates beings in.

Specialization - as explained by Frederick , in my opinion (may be
incorrect), may not bring the compelling advantages either, as there
would be already the functionality available in the "C" standard. So,
just was looking it from the larger advantages from the C++ standards
perspective.

Once again - many thanks for all your replies.

ANSTAAFL. That said, the premise is that something like
template is known to the compiler and so there is a shot at
better elbow rubbing among features, stronger type safety,
matching efficiency, etc. Also, note that to some extent,
template authoring deals with a more fine grained library
authoring, with is not always what everybody needs to be doing.
IOWs, it may not necessarily be something a beginning should be
jumping right into. As with most things there are compromises
which need to be considered and delicate balances.
 
P

Phlip

Someone said:
The only practical advantage of macros is the ability to call a function
with __FILE__ and __LINE__ for debugging.

Also use them for anything a higher-level thing can't do:

- stringerization
- token pasting
- conditional compilation

Now that we have explained why C++ templates are not C++ macros, it's time
to explain why C++ templates are "macros", according to the term's general
computer science meaning.

A template is defined to expand as if it were text, so the compiler can then
compile the result. Naturally this is more typesafe than a C++ macro. But
the actual definition of expansion leaves the intermediate text-like thing
open to subtle bugs and issues. Most notably the 'typename' keyword had to
be introduced to patch the biggest set of bugs.
 
W

wkaras

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.
 
R

Roland Pibinger

Now that we have explained why C++ templates are not C++ macros, it's time
to explain why C++ templates are "macros", according to the term's general
computer science meaning.

Hear hear!
A template is defined to expand as if it were text, so the compiler can then
compile the result. Naturally this is more typesafe than a C++ macro.

Actually, templates are untyped (what is the type of T?). The
seemingly typesafe aspect comes into play because the template macro
expansion may result in something that is not compilable, producing
the well-known heaps of error messages.

Best wishes,
Roland Pibinger
 
K

Kai-Uwe Bux

Roland said:
Hear hear!


Actually, templates are untyped (what is the type of T?).

Well, if you have

template < int T >
struct ...

then T is an int, and if you have

template < typename T >
struct ...

then T better be a typename and if you have

template < template < class > T >
struct ...

then T has to be a template taking one typename parameter.
The
seemingly typesafe aspect comes into play because the template macro
expansion may result in something that is not compilable, producing
the well-known heaps of error messages.

That too, but template parameters are typed. It just so happens that the
types typename and template are (unfortunately?) not available for run-time
variables.


Best

Kai-Uwe Bux
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,138
Messages
2,570,798
Members
47,347
Latest member
RebekahStu

Latest Threads

Top