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 : david.brown (at) *nospam* hesbynett.no (David Brown)
Groupes : comp.lang.c
Date : 14. Jul 2024, 15:17:35
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v70j5f$54u3$1@dont-email.me>
References : 1 2 3 4 5 6
User-Agent : Mozilla Thunderbird
On 13/07/2024 20:07, Richard Damon wrote:
On 7/13/24 1:56 PM, Alan Mackenzie wrote:
Hello, Richard.
>
Richard Damon <richard@damon-family.org> wrote:
On 7/13/24 1:05 PM, Alan Mackenzie wrote:
Hello, David.
>
Many thanks for the reply!  It's just what I was looking for.
>
David Brown <david.brown@hesbynett.no> wrote:
On 13/07/2024 15:04, Alan Mackenzie wrote:
Hello, comp.lang.c.

Thanks, these are all things I didn't know.  I will look them up in the
GCC manual.  :-)
>
>
In C++, I would use constexpr and static_assert to do this, and your
compiler might allow its use in C as an extension.
>
C++ isn't an option.
 The question being since most C compilers are also C++ compilers, they somethings accept this sort of C++ism as an extension.
Actually, the vast majority of C compilers are /not/ also C++ compilers.   But the most popular C compilers, by a significant margin, are C++ compilers.
_Static_assert has been in C since C11, so it does not need to be an extension - it has been standard C for over a decade.  (And many C compilers allow as extensions the use of features from later C standards to be used along with earlier standards, as well as - as you say - allowing some C++-isms.)  But to make _Static_assert work here, you'd have to stretch things a lot further than just supporting C11/C++11 static assertions.  Static assertions are a feature I like and find very useful, but they don't work in all situations I would like.

 
>
If not, in C you could use just _Static_assert, perhaps in the expansion
of a macro that generates the expression that does the testing.
>
Yes, that's an even better idea, thanks.  Looking it up in
https://en.cppreference.com, _Static_assert has existed in C since C11,
that spelling being deprecated in favour of static_assert in C23.  I just
need to check the project I'm working in doesn't still support C < C11.
>
 If the compiler doesn't support some form of static assert, you cam make one yourself with a macro.
 #define static_assert(x) extern int _static_assert[(x) ? 1 : -1]
 Possible adding whatever hacks you want to make the variable unique, (or make it a typedef, or whatever). (a typedef doesn't add an external symbol that is never used, so might be clearer, but needs something to make the name unique)
 The key idea is a array of negative size is a constraint error, so if x is false you get the error.
That's a useful technique for those stuck with pre-C11 or pre-C++11 and who don't want to use gcc-isms - I have such a macro in an oft-used header.

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