Are C++ Templates just Macros in disguise?

Solution 1:

Macros are a text substitution mechanism.

Templates are a functional turing-complete language that is executed at compile time and is integrated into the C++ type system. You can think of them as a plugin mechanism for the language.

Solution 2:

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.