I want to make a simple ostream& operator <<
overload, but I don’t want any ambiguity errors from the compiler if there so happens to be another overload.
Here is the closest thing I could think of:
template<class Collection>
std::ostream& operator << (std::ostream& stream, const Collection& other)
requires requires () {
requires not requires() {stream << other;};//errors here
} {...}
The problem with the code above is that the compiler produces the following error:
1. Satisfaction of constraint 'requires { requires !requires { stream << other; }; }' depends on itself [constraint_depends_on_self]
What I want is to make a generic overload for any collection to be easily outputed to the console (for debugging purposes), but some collections like std::string already have an overload, so a call to something like std::ostream << std::string
is ambiguous.
A simple way to mitigate the problem is to prohibit the overload for std::string
specifically, but I’m curious if it is possible to make a solution for all existing overloads, not just one specific one.
I’m using C++20 (I don’t mind C++23 solutions) clang and gcc compilers, if that matters.
1