Short answer, no. Operator overloading proceeds according to strict grammatical rules, so for any binary operator @
, the expression a @ b
considers overloaded functions (a).operator@(b)
and operator@(a, b)
.
The right thing in your case is to make a wrapper object:
template <typename T> class pass
{
std::ostream & os_;
public:
pass(std::ostream & os) : os_(os) {}
friend std::istream & operator>>(std::istream & is, pass & p)
{
T x;
if (is >> x) { p.os_ << x; }
return is;
}
friend std::istream & operator>>(std::istream & is, pass && p)
{
return is >> p;
}
};
Usage:
std::cin >> pass<int>(std::cout);
Note that we're taking the pass<T>
object by rvalue reference to allow this construction.