17-04-2025, 12:10 PM
Rule 7.0.5 presents this example:
Yes, compliant with 7.0.5. But this code then violates 7.0.6, since there is an implicit narrowing conversion from uint32_t to uint8_t in the assignment, right?
So, how are we supposed to write this type of code? Like this?
Does that really make the code safer?
Code:
u8a += static_cast< uint32_t >( u8b ) // Compliant - u8a -> unsigned int
Yes, compliant with 7.0.5. But this code then violates 7.0.6, since there is an implicit narrowing conversion from uint32_t to uint8_t in the assignment, right?
Code:
u8a = (uint32_t)(u8a) + (uint32_t)(u8b);
So, how are we supposed to write this type of code? Like this?
Code:
u8a = static_cast<std::uint8_t>(static_cast<std::uint32_t>(u8a) + static_cast<std::uint32_t>(u8b));
Does that really make the code safer?