I am using a library that is under a namespace, let's call it Hello.
namespace Hello {
template<class T>
void World(std::ostream& os, const T& t) {
os << t;
}
}
The class T
is something like std::array<int,10>
and I also write an ostream overload function for it. However, calling Hello::World(arr)
leads to compile errors that say the compiler cannot find the overload for operator<<()
.
After some search, I come up with a solution that explains this situation. How does the operator overload resolution work within namespaces? and Name resolution of functions inside templates instantiated with qualified types.
So we can simplify the situation like this.
void f(int) {}
void f(int,int) {}
namespace Hello {
using ::f;
void f(int,int,int) {}
void World()
{
f(100);
f(100, 200);
f(100, 200, 300);
}
};
int main()
{
Hello::World();
return 0;
}
Without the using ::f;
line, this code fails to be compiled since the name f
can hide ALL of the functions with the same name in the global namespace.
Now that this is my problems:
- Since
namespace Hello { .... }
is in the library, I shall not modify it. That is, I cannot modify the implementation insideWorld()
. The only solution is to put a linenamespace Hello { using ::f; }
somewhere in my code. Is it a good practice, or there is a more elegant solution? - In this example, can I only import
f(int)
and notf(int,int)
?