Deutsch English Français Italiano |
<v70j5f$54u3$1@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: David Brown <david.brown@hesbynett.no> Newsgroups: comp.lang.c Subject: Re: Is it possible to generate a compile time error from an inline function? Date: Sun, 14 Jul 2024 15:17:35 +0200 Organization: A noiseless patient Spider Lines: 68 Message-ID: <v70j5f$54u3$1@dont-email.me> References: <v6tu04$klr$1@news.muc.de> <v6u5lq$3krbn$1@dont-email.me> <v6uc4i$1rfr$1@news.muc.de> <fb9c29d418c137a633f136a36a128ea57532adad@i2pn2.org> <v6uf3v$1rfr$2@news.muc.de> <e4c6a99e1aa3a28e6f8f6176a6cc6678954156a2@i2pn2.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Sun, 14 Jul 2024 15:17:36 +0200 (CEST) Injection-Info: dont-email.me; posting-host="3132360bab10f3edba3518866baa2112"; logging-data="168899"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+C77YXaWX6vxlo2tpdpyyAvGUN6UvVa3U=" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:6axdWWD9isgPk62H+zb8wxA/Jj0= In-Reply-To: <e4c6a99e1aa3a28e6f8f6176a6cc6678954156a2@i2pn2.org> Content-Language: en-GB Bytes: 4001 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.