I have an incredibly interesting library that can translate points: it should work with any point types
template<class T> auto translate_point(T &p, int x, int y) -> decltype(px, py, void()) { px += x; py += y; } template<class T> auto translate_point(T &p, int x, int y) -> decltype(p[0], void()) { p[0] += x; p[1] += y; }
translate_point will work with points that have public members x and y , and it will also work with tuples / indexable containers, where x and y represented by the first and second elements, respectively.
The problem is that another library defines a class of points with the public x and y , but also allows indexing:
struct StupidPoint { int x, y; int operator[](int i) const { if(i == 0) return x; else if(i == 1) return y; else throw "you're terrible"; } };
My application using both libraries is as follows:
int main(int argc, char **argv) { StupidPoint stupid { 8, 3 }; translate_point(stupid, 5, 2); return EXIT_SUCCESS; }
but this makes GCC (and clang) unhappy:
error: call of overloaded 'translate_point(StupidPoint&, int, int)' is ambiguous
Now I can understand why this is happening, but I want to know how to fix it (assuming that I can not change the internals of StupidPoint), and if there is no easy way around it, how can I implement the library to deal with this more easily.
c ++ c ++ 11 sfinae
jaymmer
source share