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.cDate : 13. Jul 2024, 14: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).