Because int is implicitly constructible and assignable from a double, complete with the loss of precision. I'm sure the same is true in plain C. Annoying, yes, but that's the cost of backward compatibility. I was about to suggest using BOOST_STRONG_TYPEDEF, but the implicit conversion from double to int prevents that from helping. I think you could using the same idea as BOOST_STRONG_TYPEDEF but provide a private constructor for double and for float to prevent it from constructing an int when you try to construct with a non-integral constant.
Don't get me wrong; I completely agree that implicit type conversion is evil. That's an ugly hack to replace some evil with different evil. I'd be happy to break C compatibility for a little safety like this.
You sir, have these things called "principles". I surmise that your office believes implicitly converting to these sorts of things is evil, or at best an ugly hack. =P
Yea, like this:
#include
struct safe_int
: boost::totally_ordered1< safe_int
, boost::totally_ordered2< safe_int ( ... )
Reply
I claim (without proof) that implicit type conversion is always a Bad Idea. Languages with modern type systems agree with me.
Reply
Not that we've ever expected C++ to keep us from doing goofy things, of course.
Reply
Reply
Reply
Reply
Leave a comment