S
Steven T. Hatton
What happens when somebody just happens to define something in a library youFrederick said:Steven T. Hatton:
Did you see the macros I wrote recently in another thread:
POST_IN_TERMS_OF
OP_IN_TERMS_OF
NON_CONST_IN_TERMS_OF
These macros are very handy, and they cut down on programmer time and
error.
include in your code that breaks your macros? It happens. I recently
spent many hours trying to figure out what was wrong with code I wrote
using macros from a library provider. I started out with a working
prototype based on example code. I modified the design making reasonable
changes which one should expect a programmer to make. Among those changes
was to remove certain header files which appeared to be superfluous, and
were not indicated in the documentation as essential to the feature I was
trying to implement. When I tried to compile the code, I got errors
telling me the macro wasn't being expanded. I located the macro definitions
in a header file with a name which seemed appropriate and included that in
my code. I could see the bloody macro definition right in front of my
eyes, and it was there in a file which I was pulling into the translation
unit. Nonetheless, the macro would not expand. After many hours of trying
reasonable approaches, I tried the brute force approach of simply copying
everything from the original working prototype I could possibly put in the
modified code. Among those elements was a header file which *should* *not*
have made a difference. Nonetheless, it *did* make a difference.
What is even more bizarre is that at one point, explicitly adding a default
constructor signature to a pure abstract base class caused the damn thing
to work!
I just checked a major project I was working with about a year ago because I
was going to use it as an example of what is wrong with macro madness.
Much to my pleasant surprise the macros are GONE! In many (dozens, perhaps
hundreds of) cases they have been replaced with templates.
I believe there is a place for "cookie cutter" code. What Cpp provides is a
means of crossing the boundary between code text, and compiled code. It is
a form of recursive self-reference, if you will pardon the redundancy.
That is actually a very powerful ability. Unfortunately it's provided by a
very weak tool.
This represents one of the most elegant (if the term can even be applied to
macros) uses of Cpp I've ever seen. I once took the entire thing and
turned it into normal source code with about 25 keystrokes using Emacs
regexp search and replace.
http://www.kdevelop.org/HEAD/doc/api/html/ast_8h-source.html
When I see macros, I consider the time I have spent trying to fix Cpp
related problems, and they never seem to have the potential for saving that
much time.