Sujet : Re: Is it possible to generate a compile time error from an inline function?
De : 643-408-1753 (at) *nospam* kylheku.com (Kaz Kylheku)
Groupes : comp.lang.cDate : 14. Jul 2024, 23:55:56
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <20240714153502.59@kylheku.com>
References : 1 2 3
User-Agent : slrn/pre1.0.4-9 (Linux)
On 2024-07-14, Alan Mackenzie <
acm@muc.de> wrote:
I wanted to generate code conditionally, depending on the macro's
arguments, for efficiency's sake. I don't think this is possible - the C
preprocessor is not Lisp. What I missed was that the compiler's
optimizer will eliminate the superfluous code anyway, so it doesn't
really matter.
The C preprocessor is a kind of token-churning functional language.
One way it can make decisions is by token-pasting together the name
of a macro.
$ gcc -E -
#define FOO_OR_BAR(ARG, WHICH) MAC ## WHICH(ARG)
#define MAC0(ARG) FOO(ARG)
#define MAC1(ARG) BAR + ARG
FOO_OR_BAR(42, 0)
FOO_OR_BAR(73, 1)
// [Ctrl-D][Enter] here
# 1 "<stdin>"
# 1 "<built-in>"
# 1 "<command-line>"
# 31 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 32 "<command-line>" 2
# 1 "<stdin>"
FOO(42)
BAR + 73
A couple of years ago I made a project called "cppwawk" which combined
Awk with the GNU C preprocessor.
https://www.kylheku.com/cgit/cppawk/about/It has a small library of headers including a multi-clause loop macro
that supports parallel and nested iteration, and programmer-definable
clauses!
#include <iter.h>
BEGIN {
loop (from_step (vel, 5, -1),
from_step (pos, 0, vel),
while (pos >= 0),
maximizing (maxpos, pos))
{
print pos
}
print "maxpos =", maxpos
}
Output:
0
4
7
9
10
10
9
7
4
0
maxpos = 10
-- TXR Programming Language: http://nongnu.org/txrCygnal: Cygwin Native Application Library: http://kylheku.com/cygnalMastodon: @Kazinator@mstdn.ca