Re: Is it possible to generate a compile time error from an inline function?

Liste des GroupesRevenir à l c 
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.c
Date : 15. Jul 2024, 00: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/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Date Sujet#  Auteur
13 Jul 24 * Is it possible to generate a compile time error from an inline function?15Alan Mackenzie
13 Jul 24 +* Re: Is it possible to generate a compile time error from an inline function?10David Brown
13 Jul 24 i`* Re: Is it possible to generate a compile time error from an inline function?9Alan Mackenzie
13 Jul 24 i `* Re: Is it possible to generate a compile time error from an inline function?8Richard Damon
13 Jul 24 i  +* Re: Is it possible to generate a compile time error from an inline function?6Alan Mackenzie
13 Jul 24 i  i`* Re: Is it possible to generate a compile time error from an inline function?5Richard Damon
14 Jul 24 i  i +- Re: Is it possible to generate a compile time error from an inline function?1Kenny McCormack
14 Jul 24 i  i `* Re: Is it possible to generate a compile time error from an inline function?3David Brown
14 Jul 24 i  i  `* Re: Is it possible to generate a compile time error from an inline function?2Keith Thompson
14 Jul 24 i  i   `- Re: Is it possible to generate a compile time error from an inline function?1David Brown
14 Jul 24 i  `- Re: Is it possible to generate a compile time error from an inline function?1David Brown
13 Jul 24 `* Re: Is it possible to generate a compile time error from an inline function?4Tim Rentsch
14 Jul 24  `* Re: Is it possible to generate a compile time error from an inline function?3Alan Mackenzie
15 Jul 24   +- Re: Is it possible to generate a compile time error from an inline function?1Kaz Kylheku
16 Jul 24   `- Re: Is it possible to generate a compile time error from an inline function?1Tim Rentsch

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal