Are C++ Templates just Macros in disguise?
- by Roddy
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.
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.
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.
So, what ARE the real distinctions? and how can templates avoid the dangers that #define leads you into, like
Inscrutable compiler errors in
places where you don't expect them?
Code bloat?
Difficulty in tracing code?
Setting Debugger Breakpoints?