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

Liste des GroupesRevenir à l c 
Sujet : Is it possible to generate a compile time error from an inline function?
De : acm (at) *nospam* muc.de (Alan Mackenzie)
Groupes : comp.lang.c
Date : 13. Jul 2024, 15:04:04
Autres entêtes
Organisation : muc.de e.V.
Message-ID : <v6tu04$klr$1@news.muc.de>
User-Agent : tin/2.6.3-20231224 ("Banff") (FreeBSD/14.0-RELEASE-p5 (amd64))
Hello, comp.lang.c.

What I want to do is check the validity of (constant) arguments to an
inline function, and output a compiler error if they are invalid.

In particular, I have:

    u32 __always_inline ACM_BITFIELD (u8 a[], int offset, int length)

, which is to extract a bitfield of LENGTH bits, starting at bit number
OFFSET in the array of bytes A.  OFFSET and LENGTH will be known at
compile time.

For the sake of run time efficiency, I wish to impose the restrictions
that either (i) the bitfield will be contained entirely within a byte; or
(ii) the bitfield will be a number of consecutive whole bytes (maximum 32
bits).

So, for example, if the code called

    foo = ACM_BITFIELD (bar, 14, 4);

, I would like to output the compiler message "Invalid arguments 14, 4,
to ACM_BITFIELD", since this bitfield straddles two bytes.

Is there any way I can do this in C?  (Before anybody asks, yes I have
looked at doing it with macros, but that seems impractical, if it's even
possible.)

Thanks!

--
Alan Mackenzie (Nuremberg, Germany).


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