Sh4*_*4pe 5 c++ templates operator-overloading friend nested-class
我有以下设置:
template< class T >
struct Foo {
  struct Bar {
    Bar ( const T &t ) : otherT_( t ) {}
    T otherT_;
  };
  Foo ( const T &t ) : myT_( t ) {}
  T myT_;
};
现在,我想制作Foo< T >::Barstremable到std :: cout和朋友的实例.我试过这个:
template< class T >
std::ostream& operator<< ( std::ostream &os, 
                           const typename Foo< T >::Bar &bar ) {
  os << "<bar: " << bar.otherT_ << ">";
  return os;
}
但是以下代码无法编译:
  Foo< int > foo( 5 );
  Foo< int >::Bar bar( 7 );
  std::cout << bar << std::endl;
我想编译器无法推断出类型T或其他东西.有没有办法使嵌套类的这种实例表现良好operator<<?
谢谢!
Raf*_*ffi 10
是的,简单的方法是放入operator<<内部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;
  }
};
我正在挖掘另一种方式......
| 归档时间: | 
 | 
| 查看次数: | 1366 次 | 
| 最近记录: |