Home > Blockchain >  C always use checked_cast instead of static_cast?
C always use checked_cast instead of static_cast?

Time:02-15

When I read about C casting operators, I see generally see 4 types of casts for example here: Cast types

const_cast
dynamic_cast
reinterpret_cast
static_cast

But what about checked_cast as described here checked_cast

Should we always use checked_cast instead of static_cast as a rule of thumb?

CodePudding user response:

checked_cast only replaces static_cast.

That article assumes that you never want a lossy conversion. If you have a float and want the largest int not larger than that, checked_cast is incorrect.

It also assumes that the conversion goes both ways. If it doesn't then it's ill formed.

  • Related