If you implement <<
after class:
#include <iostream> template <typename t_b> class B; template <typename t_a> class A { public: friend class B<t_a>; operator B<t_a>(); }; template <typename t_b> class B { public: friend class A<t_b>; A<t_b> operator [] (const B &s2) {} friend std::ostream& operator << <t_b>(std::ostream &, const B &); // <-- ошибка тут }; template<typename t_a> A<t_a>::operator B<t_a>() { return B<t_a>(); } template <typename t_b> std::ostream& operator << (std::ostream &os, const B<t_b> &s) {} int main() { B<int> s1, s2, s3; std::cout << s1[s2]; }
[Error] declaration of 'operator <<' as non-function
If before:
#include <iostream> template <typename t_b> class B; template <typename t_a> class A { public: friend class B<t_a>; operator B<t_a>(); }; template <typename t_b> std::ostream& operator << (std::ostream &os, const B<t_b> &s) {} template <typename t_b> class B { public: friend class A<t_b>; A<t_b> operator [] (const B &s2) {} friend std::ostream& operator << <t_b>(std::ostream &, const B &); }; template<typename t_a> A<t_a>::operator B<t_a>() { return B<t_a>(); } int main() { B<int> s1, s2, s3; std::cout << s1[s2]; // <-- ошибка тут }
[Error] cannot bind 'std :: ostream {aka std :: basic_ostream}' lvalue to 'std :: basic_ostream &&'
Judging by the answers Operator overloading >> and << in the template class as friend functions and an error occurs with implicit type conversion in the first case there is a gcc
bug, and in the second - a problem with ADL
. And the solutions are mutually exclusive. How to be?