Search code examples
c++ambiguous

Dealing with ambiguous declarations in C++


In my header file I define:

inline t_pair pair(int a, int b) { t_pair p; p.a = a; p.b = b; return p; }

But I get a compiler error "Reference to 'pair' is ambiguous". Apparently there is a

struct _LIBCPP_TYPE_VIS_ONLY pair

defined in utility.cpp, which I do not include directly.

Is there a way to still use my pair function without renaming it?


Solution

  • As already mentioned, you can wrap your function declaration with another namespace or you can just use the std::pair class and avoid reinventing the wheel.

    Also note that the std::pair class allows you to create pairs of generic types. So it's not limited to only pairs of type int. You can find an example of its usage here.