Given two namespaces which each provide a specialization of operator<< for std::vector, is it possible to use boost::lexical_cast? I know the code will work if I promote one of the operators into the global namespace, but that just causes an ambiguity error in other locations. Is there some clever usage of the "using" directive that I can use to allow the boost::lexical_cast to find the right operator?
//In some .h file
namespace A
{
template <typename T, typename A>
std::ostream & operator<<( std::ostream & os, const std::vector<T, A> & v)
{
...
}
}
namespace B
{
template <typename T, typename A>
std::ostream & operator<<( std::ostream & os, const std::vector<T, A> & v)
{
...
}
}
//Later in a .cpp
namespace A
{
std::vector<int> v;
std::string s = boost::lexical_cast<std::string>(v); //Fails because operator<< is not defined for std::vector in the std namespace
}
namespace B
{
std::stringstream stream;
std::vector<int> v;
stream << v; //This will be ambiguous if we promote the A::operator<< into the std namespace
}
Edit: So far the best I've come up with is to pull the operator into the std namespace in the .cpp. This works if the .cpp only needs one version, but not in the general case where the .cpp needs multiple versions.
namespace std
{
using A::operator<<;
}
You can use a nameless namespace (untested with
lexical_cast
but works with other things):