Antoine
Antoine

Reputation: 5198

Overloading operator<<: cannot bind lvalue to ‘std::basic_ostream<char>&&’

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;
}

Can someone tell me why this code is not legal, and what's the best way to do what I want?

Upvotes: 42

Views: 55396

Answers (3)

Bo provided the reason why this is happening (the type T is not deducible in the call to the nested operator<<. 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:

template<typename T>
struct classA {
  struct classB
  {
    friend inline std::ostream& operator<< (std::ostream &out,
                                            const classB &b) {
       // definition goes here
    }
  };

  classB root;

  friend std::ostream& operator<< (std::ostream &out,
                                   const classA<U> &tree) {
       // definition goes here
  }
};

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 of operator<< for the compiler to consider when the argument is not ClassA<T> or ClassA<T>::ClassB.


How access can be gained with your approach

namespace {
   struct intruder {
       ClassA & ref;
       intruder( ClassA& r ) : ref(r) {}
   };
   template <>
   std::ostream& operator<< <intruder>( std::ostream& _, ClassA<intruder> const& i ) {
       std::cout << i.ref.private_member << std::endl;
       return _;
   }
}

Alternative

Alternatively you can befriend a particular specialization of a template. That will solve the intruder problem, as it will only be open to operator<< to ClassA<intruder>, which has a much lesser impact. But this will not solve your particular issue, as the type would still not be deducible.

Upvotes: 23

dsign
dsign

Reputation: 12710

Try this:

template<typename T>
inline std::ostream& operator<< (std::ostream &out,
                             const classA<T> &tree)
{
   //out << tree.root;
   ::operator<<( out, tree.root);
   return out;
}

and then you will get a straightforward confession of ineptitude:

test.cpp:34:3: error: no matching function for call to ‘operator<<(std::ostream&, const classA<int>::classB&)’
test.cpp:34:3: note: candidates are:
test.cpp:23:22: note: template<class T> std::ostream& operator<<(std::ostream&, const     typename classA<T>::classB&)
test.cpp:30:22: note: template<class T> std::ostream& operator<<(std::ostream&, const classA<T>&)

Workaround: maybe you can use a member function in nested classB, and use it instead of operator<< ... Of course, that solution has a multitude of drawbacks, but it may get you out of this hurry.

Upvotes: 2

Bo Persson
Bo Persson

Reputation: 92381

You have a problem with a "non-deducible context" in this operator

template<typename T>
inline std::ostream& operator<< (std::ostream &out,
                                 const typename classA<T>::classB &b)
{
  return out;
}

The compiler cannot figure out what values of T will result in a classB that matches the parameter you want to pass. So this template is not considered!

In C++11 mode, the compiler then goes on to find a close match from the standard library

operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&)

where it can match _Tp to just about any type, including classA<T>::classB, but notes that the first parameter doesn't match.

Upvotes: 29

Related Questions