Deutsch English Français Italiano |
<vfqsgm$1ji6h$1@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!eternal-september.org!feeder2.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Thiago Adams <thiago.adams@gmail.com> Newsgroups: comp.lang.c Subject: Re: constexpr keyword is unnecessary Date: Tue, 29 Oct 2024 11:46:46 -0300 Organization: A noiseless patient Spider Lines: 74 Message-ID: <vfqsgm$1ji6h$1@dont-email.me> References: <veb5fi$3ll7j$1@dont-email.me> <877ca5q84u.fsf@nosuchdomain.example.com> <vf0ijd$3u54q$1@dont-email.me> <vf0l98$3un4n$1@dont-email.me> <vf1216$p0c$1@dont-email.me> <87y12jpxvl.fsf@nosuchdomain.example.com> <vf1d2o$2hjk$1@dont-email.me> <87plnvpgb9.fsf@nosuchdomain.example.com> <vf2sm8$deou$1@dont-email.me> <vf7m4s$1d8mj$1@raubtier-asyl.eternal-september.org> <vf86uc$1fvt3$1@dont-email.me> <vfit29$3obkb$1@dont-email.me> <vfj5up$3q2lf$1@dont-email.me> <20241027220459.109@kylheku.com> <vfnu92$vp1g$1@dont-email.me> <20241028215919.996@kylheku.com> <vfqjnk$1hos6$1@dont-email.me> <vfqjvn$1hos6$2@dont-email.me> <vfqqhs$1j1i2$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Tue, 29 Oct 2024 15:46:47 +0100 (CET) Injection-Info: dont-email.me; posting-host="55498c66dcd053969c17c2605d166640"; logging-data="1689809"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/LW1g1dQkV53aF5Mbk0k2+R5CoW5svUpM=" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:QCmC/WvuvE83DfapM13ge8jjle4= Content-Language: en-US In-Reply-To: <vfqqhs$1j1i2$1@dont-email.me> Bytes: 3545 On 29/10/2024 11:13, David Brown wrote: > On 29/10/2024 13:21, Thiago Adams wrote: >> On 29/10/2024 09:16, Thiago Adams wrote: >> >> Edit to fix the sample >> >> // Here we acknowledge the truncation >> void f(int i) { >> ... >> const unsigned char c = i; [[!truncation]]; >> } >> >> >> // Warning: there is no truncation to acknowledge. >> void f(unsigned char i) { >> ... >> const unsigned char c = i; [[!truncation]]; >> } >> > > This should give you what you are asking for, if I have understood you > correctly: > > #define truncate(type, value) \ > _Generic((value), \ > type : (void) 0, \ > default : (type) value \ > ) > > > > // OK > void f1(int i) { > const auto c = truncate(unsigned char, i); > } > > // Error > void f1(unsigned char i) { > const auto c = truncate(unsigned char, i); > } > > > I am not at all convinced this is a good idea. I am /certainly/ not > convinced "truncate" is a good name - the general term, AFAIK, for a > conversion that might try to squeeze a large value into a smaller type > is "narrowing" rather than "truncating". > > You could expand the idea further and have a "truncate" macro that > checks for bounds at run time or compile time (I'd make use of the gcc > extension __builtin_constant_p here, but there may be a fully standard > way to do this). > > As I said, >(I intend to show a general idea. There are likely better examples.) My objective was to show the concept of "warning acknowledge". Name and syntax weren't important. The solution for truncate you are proposing maybe is valid, but it is for the specific case. I think it is valid to think is specific cases and I can find more samples. maybe if (condition) where the condition is constant expressions.