The original designer of C once described templates as a cle

The original designer of C++ once described templates as “a clever kind of macro that obeys the scope, naming, and type rules of C++”. How close is the similarity? What can templates do that macros can’t? What do macros do that templates don’t?

Solution

1)Templates undoubtedly take longer time than macros.

2)Templates are significantly more powerful and obey C++ syntactical rules, whereas macros do not.

3)The reason Templates take longer is because you can have a template which is recursive, and all of those recurrences need to be generated. This is the foundation upon which looping constructs in Template Metaprogramming are built. Macros, in contrast, cannot call themselves, and are therefore limited to a single expansion.

4)Templates cause code substitution, but obey a certain set of rules. Which means that the compiler checks types before creating the new class or object. Macro are parsed by the Pre-processor, which only replaces text.

The original designer of C++ once described templates as “a clever kind of macro that obeys the scope, naming, and type rules of C++”. How close is the similari

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site