重载运算符<<用于嵌套类模板

Overload operator<< for nested class template

本文关键字:lt 嵌套 重载 用于 运算符      更新时间:2023-10-16

我有以下设置:

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 >::Bar实例可以流式传输到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<<中表现良好?

谢谢!

是的,简单的方法是将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;
  }
};

我正在以另一种方式挖掘...

解决方法 - 在Bar的定义中将operator<<定义为朋友:

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

正如 KerrekSB 在评论中所说,您的方法的问题在于无法推断出 T。Foo<T>::Bar可能有无限多个T,每个也可能导致不同的类型。

编译器无法推断 T,但是,当您将其设为朋友时,它会通过 ADL 找到它。

我已将代码修改为以下内容:

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