Liste des Groupes | Revenir à cl c |
On 29/10/2024 13:21, Thiago Adams wrote:As I said,On 29/10/2024 09:16, Thiago Adams wrote:This should give you what you are asking for, if I have understood you correctly:
>
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]];
}
>
#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).
Les messages affichés proviennent d'usenet.