Path: ...!weretis.net!feeder8.news.weretis.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Kaz Kylheku <643-408-1753@kylheku.com> Newsgroups: comp.lang.c Subject: Re: Is it possible to generate a compile time error from an inline function? Date: Sun, 14 Jul 2024 22:55:56 -0000 (UTC) Organization: A noiseless patient Spider Lines: 70 Message-ID: <20240714153502.59@kylheku.com> References: <86ikx96s8n.fsf@linuxsc.com> Injection-Date: Mon, 15 Jul 2024 00:55:57 +0200 (CEST) Injection-Info: dont-email.me; posting-host="cf4046d13f74d7012fcb7650b259afb8"; logging-data="370247"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18AFg0CBPoU1GSKXXWxdoo/KQIhRoEdxso=" User-Agent: slrn/pre1.0.4-9 (Linux) Cancel-Lock: sha1:OzVk6T7AILwDzAfPSSN5AkVkKNc= Bytes: 2579 On 2024-07-14, Alan Mackenzie 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 "" # 1 "" # 1 "" # 31 "" # 1 "/usr/include/stdc-predef.h" 1 3 4 # 32 "" 2 # 1 "" 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 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/txr Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal Mastodon: @Kazinator@mstdn.ca