How to force function parameter to be the same type and not allow using the type constructor to match the given type?

c++, visual-c++, visual-studio-2008

Solution

Make the `XY` constructor explicit:

explicit XY(int v) : x(v), y(v) {}

This will disallow implicit conversions from `int` to `XY`, which is what is happening when you call the single-parameter `test1` function.

Problem

I was a bit surprised finding out this feature in C++, and I didn't expect it to happen. Here is the code: ``` struct XY { int x,y; XY(int v) : x(v), y(v) {} }; bool test1(const XY &pos){ return pos.x < pos.y; } bool test1(int x, int y){ return x < y; } void functest(){ int val = 5; test1(val); } ``` So I can call a function with integer parameter, whether or not such overload exists, it will use the XY type function because it has a constructor of that same type! I don't want that to happen, what can I do to prevent this?

Original source