templates like macros?

Started by
3 comments, last by ironfroggy 20 years, 3 months ago
Can templates do more than just create classes and functions for multiple data types? can they do more generalized replacements like macros, and are there any template conditionals like the preprocessor #ifdef?
(http://www.ironfroggy.com/)(http://www.ironfroggy.com/pinch)
Advertisement
See the book "Modern C++ Design."
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
I''m not exactly sure if this is what you''re asking, but templates can do things like type switching, which can sort of act like preprocessor conditionals. However, the syntax for doing so is often less elegant. On the other hand, you get better type safety. This also comes with the caveat that there already different compiler/C++ features that can be used to replace preprocessor conditionals depending on the circumstances. For example, in most cases I prefer normal if statements on constants to #ifdef.

If you want a more specific explanation, try giving a specific example.
You can do template metaprogramming, which is fun!

Enigma
Look up template metaprogramming. With a compiler that supports templates properly (*cough*VC6*cough*), they can be amazing.

edit: Two seconds late.. I must be getting old.

[edited by - psykr on January 24, 2004 1:52:57 PM]

This topic is closed to new replies.

Advertisement