c++ friend operator template specialization
问题 I have a generalized modulo struct called quotient_ring . The relevant bits are shown below. template <typename R = long long> struct quotient_ring{ using Q = quotient_ring; R x, m; ... template <typename T> friend constexpr std::basic_ostream<T> &operator<< (std::basic_ostream<T> &str, const Q &q){ return str << '(' << q.x << ")%(" << q.m << ')'; } }; This operator << would print something like 2 mod 7 as (2)%(7) . The reason I need the brackets is because the type R can become very nested.