Archived

This topic is now archived and is closed to further replies.

ironfroggy

templates like macros?

Recommended Posts

ironfroggy    122
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?

Share this post


Link to post
Share on other sites
SiCrane    11839
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.

Share this post


Link to post
Share on other sites
psykr    295
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]

Share this post


Link to post
Share on other sites