Overload operator<< for nested class template

寵の児 提交于 2020-01-11 08:51:09

问题


I have the following setup:

template< class T >
struct Foo {

  struct Bar {
    Bar ( const T &t ) : otherT_( t ) {}

    T otherT_;
  };

  Foo ( const T &t ) : myT_( t ) {}

  T myT_;
};

Now, I want to make instances of Foo< T >::Bar streamable to std::cout and friends. I tried this:

template< class T >
std::ostream& operator<< ( std::ostream &os, 
                           const typename Foo< T >::Bar &bar ) {
  os << "<bar: " << bar.otherT_ << ">";
  return os;
}

But the following code does not compile:

  Foo< int > foo( 5 );
  Foo< int >::Bar bar( 7 );

  std::cout << bar << std::endl;

I guess that the compiler is not able to deduce the type T or something. Is there a way to make such instances of the nested class behave well with operator<<?

Thank you!


回答1:


Yep, the easy way is to put operator<< inside Bar:

struct Bar {
  Bar ( const T &t ) : otherT_( t ) {}

  T otherT_;

  friend std::ostream& operator<< ( std::ostream &os, const Bar &bar ) 
  {
    os << "<bar: " << bar.otherT_ << ">";
    return os;
  }
};

I am digging the other way ...




回答2:


Workaround - define operator<< as a friend inside Bar's definition:

template< class T >
struct Foo {

  struct Bar {
    Bar ( const T &t ) : otherT_( t ) {}

    T otherT_;

    friend std::ostream& operator<< ( std::ostream &os, const Bar &bar )
    {
      os << "<bar: " << bar.otherT_ << ">";
      return os;
    }

  };

  Foo ( const T &t ) : myT_( t ) {}

  T myT_;
};

The problem with your approach is, as KerrekSB said in comments, that T could not be deduced. There are possibly infinitely many T for Foo<T>::Bar, each of which could result in a different type, too.




回答3:


The compiler cannot deduce T, however, when you make it a friend it finds it via ADL.

I've modified the code to the following:

#include <iostream>
using namespace std;

template< class T >
struct Foo {

struct Bar {
Bar ( const T &t ) : otherT_( t ) {}

T otherT_;
friend std::ostream& operator << (std::ostream& os, const Bar &bar )
{ return os; }
};

Foo ( const T &t ) : myT_( t ) {}


T myT_;
};

int main() {
Foo< int > foo( 5 );
Foo< int >::Bar bar( 7 );

std::cout << bar << std::endl;
return 0;
}


来源:https://stackoverflow.com/questions/18823618/overload-operator-for-nested-class-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!