Deutsch English Français Italiano |
<vfqqhs$1j1i2$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: David Brown <david.brown@hesbynett.no> Newsgroups: comp.lang.c Subject: Re: constexpr keyword is unnecessary Date: Tue, 29 Oct 2024 15:13:16 +0100 Organization: A noiseless patient Spider Lines: 52 Message-ID: <vfqqhs$1j1i2$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> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Tue, 29 Oct 2024 15:13:17 +0100 (CET) Injection-Info: dont-email.me; posting-host="2bba4cf2cbecbf808c22a66c9df2e62e"; logging-data="1672770"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18auzDlsWciAMxjHwr22EABCAJY5wHkWus=" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.11.0 Cancel-Lock: sha1:gtzSfMMpuNWcLE30jJflwTsKp8A= Content-Language: en-GB In-Reply-To: <vfqjvn$1hos6$2@dont-email.me> Bytes: 2959 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).