Function overloading organisation and warnings.

In my game engine I have functions which require two int values. Let's use setPosition for demonstration.

setPosition(int x, int y);

When I call it like this setPosition(3, 3.2f);
I get an warning saying data loss is possible and that's because there is a float instead of int. And because it will annoy people I came to the solution to overload function with all possible combinations of int and float. So I made this.

1
2
3
4
setPosition(int x, int y);
setPosition(int x, float y);
setPosition(int float x, int y);
setPosition(int float x, float y);


Everything was fine until I started applying it to other functions, there were so many combinations that I asked myself is there a better way to do it or should I? Should I just make one combination of setPosition.
I know this question is little silly, but I'm very optimistic when it comes to my code. Thanks :).
Last edited on
If the function works with integer coordinates, and all the overloads just cast to int internally, I think you should only provide the setPosition(int,int) version.

The C++ standard does not mandate that a warning should be emitted in these situation. The warning is just a feature of your compiler and can probably be turned on or off if you want.

If someone compiles your code with these warnings enabled you should assume that they want to be warned in situations like this. If the conversion from float to int is not a mistake they would make an explicit cast (e.g. setPosition(3, static_cast<int>(3.2f));) to disable the warning.
Last edited on
Thank you very much!
Topic archived. No new replies allowed.