The thing you have to remember is that every float
-to-int
conversion and vice versa potentially loses information. On most implementations, int
can store a larger integer number with full precision than float
, while float
's decimal values are chopped off when converting to an int
and that float
can store larger numbers than can fit into an int
.
Now for your specific use case, that may be perfectly fine or more specifically is exactly what you want. But in general, one should not take such conversions lightly.
Consider just the example from your SO question, which doesn't even involve float
conversion:
int size = vector.size(); // Throws an implicit conversion warning
int size = (int)vector.size(); // C like typecasting is discouraged and forbidden in many code standards
int size = static_cast<int>vector.size(); // This makes me want to gauge my eyes out (it's ugly)
Each of these cases has a subtle bug: if the size of the vector
is greater than numeric_limits<int>::max()
(typically 2^31 - 1), then you aren't getting the actual size. So whatever you're doing with size
is not going to work.
Now, you can say that you won't have a vector
that big. And that may genuinely be true... today. How many security holes/bugs have been opened up because an application scaled to the point where some value overflowed the expected type? And how many bugs exist that are out there, lurking, waiting to pounce once some arbitrary size is exceeded?
That's why you get a warning when you don't explicitly convert it. That's why C++ uses syntax that "makes me want to gauge my eyes out". It's because what you're doing may not be safe. So you should carefully consider whether you ought to be doing it.
And that's where we get to:
3D voxelization of geometry; 3D reconstruction of 2D textures and subsequent UV mapping onto the original texture to store values; Conversion of float coordinates to grid cells for the cached version of perlin noise...
Each of those things should be hidden behind some interface which internally does the required conversion. And not a simplistic convert_int
function; I mean one that is specific to the task in question. If you're converting normalized [0, 1] floats into pixel coordinates for a texture of some size, you have a function to do exactly that. It would be given the coordinates and the size of the texture, and it would return integer coordinates.
The point of the advice is that your code should not be littered with such naked conversions.
void*
? Etc. There are many kinds of explicit conversions. Your question is predicated on something which we have no real idea of or foundation for.T
to pointers toU
and the like. Converting between integers and floats is not. You asked about a general thing and got a general answer, when what you wanted was a specific thing.