<p>I've been programming in C++ for a few years, and I've used STL quite a bit and have created my own template classes a few times to see how it's done.</p>

<p>Now I'm trying to integrate templates deeper into my OO design, and a nagging thought keeps coming back to me: They're just a macros, really... You could implement (rather UGLY) auto_ptrs using #defines, if you really wanted to.</p>

<p>This way of thinking about templates helps me understand how my code will actually work, but I feel that I must be missing the point somehow. Macros are meant evil incarnate, yet "template metaprogramming" is all the rage.</p>

<p>So, what ARE the real distinctions? and how can templates avoid the dangers that #define leads you into, like</p>

<ul>
<li>Inscrutable compiler errors in
places where you don't expect them?</li>
<li>Code bloat? </li>
<li>Difficulty in tracing code?</li>
<li>Setting Debugger Breakpoints?</li>
</ul>

They are parsed by the
compiler
and not by a preprocessor that runs
before
the compiler.
Here's what MSDN says about it:
http://msdn.microsoft.com/en-us/library/aa903548(VS.71).aspx
Here are some problems with the macro:
There is no way for the compiler to verify that the macro parameters are of compatible types.
The macro is expanded without any special type checking.
The i and j parameters are evaluated twice. For example, if either parameter has a postincremented variable, the increment is performed two times.
Because macros are expanded by the preprocessor, compiler error messages will refer to the expanded macro, rather than the macro definition itself. Also, the macro will show up in expanded form during debugging.
If that's not enough for you, I don't know what is.