I have a class that uses a nested class, and want to use the nested class operator<< to define operator<< in the upper class. Here is how my code looks like:
#include <memory>
#include <iostream>
template<typename T>
struct classA {
struct classB
{
template<typename U>
friend inline std::ostream& operator<< (std::ostream &out,
const typename classA<U>::classB &b);
};
classB root;
template<typename U>
friend std::ostream& operator<< (std::ostream &out,
const classA<U> &tree);
};
template<typename T>
inline std::ostream& operator<< (std::ostream &out,
const classA<T> &tree)
{
out << tree.root;
return out;
}
template<typename T>
inline std::ostream& operator<< (std::ostream &out,
const typename classA<T>::classB &b)
{
return out;
}
int main()
{
classA<int> a;
std::cout << a;
}
-
When compiling without support for C++11, the definition of operator<< for the inner class seems not to be found by the compiler:
so.hpp:24:7: error: no match for ‘operator<<’ in ‘out << tree.classA<int>::root’ so.hpp:24:7: note: candidates are: ... -
With GCC 4.6 and 4.7 when compiling with std=c++0x:
so.hpp:21:3: error: cannot bind ‘std::ostream {aka std::basic_ostream<char>}’ lvalue to ‘std::basic_ostream<char>&&’ In file included from /usr/include/c++/4.7/iostream:40:0, from so.hpp:2: /usr/include/c++/4.7/ostream:600:5: error: initializing argument 1 of ‘std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Tp = classA<int>::classB]’
Can someone tell me why this code is not legal, and what’s the best way to do what I want?
Bo provided the reason why this is happening (the type
Tis not deducible in the call to the nestedoperator<<. A simple workaround for this, and something that I recommend in general, not only here, is not befriending a template, but rather a single free function. For that you will need to define the function inline:There are a couple of differences among the two approaches. The most important one is that this approach will have the compiler define a non-templated overload for
operator<<for each instantiation of the template, which because it is no longer a template, does not depend on deducing the arguments. Another side effects are that the approach is a little tighter (you are only befriending one function, while in your initial approach you befriended the template and all possible instantiations (which can be used as a loophole to gain access to your class internals). Finally the functions so defined will only be found through ADL, so there are less overloads ofoperator<<for the compiler to consider when the argument is notClassA<T>orClassA<T>::ClassB.How access can be gained with your approach
Alternative
Alternatively you can befriend a particular specialization of a template. That will solve the
intruderproblem, as it will only be open tooperator<<toClassA<intruder>, which has a much lesser impact. But this will not solve your particular issue, as the type would still not be deducible.