4 ms·
Here's how to do it in D: struct ListNode(T) { ListNode* next; T data; } T!int node; Why suffer the C preprocessor? Using preproc
by WalterBright 1y ago
Here's how to do it in D:
struct ListNode(T) {
ListNode* next;
T data;
}
T!int node;
Why suffer the C preprocessor? Using preprocessor macros is like using a hammer for finish carpentry, rather than a nail gun. A nail gun is 10x faster, drives the nail perfectly every time, and no half moon dents in your work.
- dhooper 1y agoThanks, this post is about C. On some projects you must use C.
- WalterBright 1y agoIf I may may be provocative :-) this post isn't about C. It's about layering on a custom language using C preprocessor macros. My compilers were originally written in C. I started using the C preprocessor to do metaprogramming. After some years I got fed up with it and removed nearly all of the preprocessor use, and never looked back. My code was much easier to understand. An amusing story: long ago, a friend of mine working for Microsoft was told by a team leader that a 50K program had a bug in it, and sadly the developer was long gone. He'd assigned programmer after programmer to it, who could not fix it. My friend said he'd give it a try, and had it fixed in 2 hours. The source code was written in Microsoft MASM, where the M stood for "Macro". You can guess where this is going. The developer had invented his own high level language using the macro system (which was much more powerful than C's). Unfortunately, he neglected to document it, and the other programmers spent weeks studying it and could not figure it out. The leader, astonished, asked him how he figured it out in 2 hours? My friend said simple. He assembled it to object code, then disassembled the object code with obj2asm (a disassembler I wrote that converts object code back to source code). He then immediately found and fixed the bug, and checked in the "new" source code which was the disassembled version. I've seen many very smart and clever uses of the C macros, the article is one of them. But isn't it time to move on?
- ryao 1y agoIf the C compiler accepts it, it is C.
- zabzonk 1y agoThere is no one "the C compiler".
- WalterBright 1y agoPedantically, the preprocessor is an entirely separate language. The lexing, parsing, expressions, and semantics are totally distinct. The preprocessor is usually implemented as a completely independent program. My first C compiler did integrate the preprocessor with the C compiler, but that was for performance reasons. Currently, ImportC runs cpp and then lexes/parses the resulting C code for use in D.
- ryao 1y agoIt is part of the C standard. Whether it is part of a separate binary is an implementation choice.
- WalterBright 1y agoTrue on both counts. But they are still separate and distinct languages.
- dfawcus 1y agoNah - one would simply use a punch with the hammer. So nail the architrave with the hammer until the nail is say 1/8" proud, then punch it home.
- WalterBright 1y agoThere's a reason carpenters use nail guns. Try it and you'll see!
- dfawcus 1y agoWell, obviously speed when one is on the clock. All power tools have trade offs vs the manual alternates, often tipping towards their use, but not always. I have had success on an occasion using a belt fed screw gun when fitting plaster boards, especially for ceiling boards. However I don't do joinery often enough to justify the use of a nail gun.
- WalterBright 1y agoI tried installing baseboard with a hammer, and then a nail gun. The latter won hands down in speed, consistency, no bent nails, and minimal spackling. Another thing - a hammer requires 3 hands: 1 for the hammer, one for the nail, one for holding the piece in position. A nail gun only requires 2 hands.