Liste des Groupes | Revenir à cl c |
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.
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.The question being since most C compilers are also C++ compilers, they somethings accept this sort of C++ism as an extension.>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.
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.>If the compiler doesn't support some form of static assert, you cam make one yourself with a macro.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.
>
#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.
Les messages affichés proviennent d'usenet.