【发布时间】:2013-09-20 08:14:20
【问题描述】:
我有以下设置:
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<< 一起表现良好?
谢谢!
【问题讨论】:
-
复制/粘贴this answer?
-
@KerrekSB 所以答案是不可能简单地写成
std::cout << bar?或者我可以明确链接答案中提到的一对一映射吗? -
@KerrekSB 我不确定我是否遵循引用的答案。他的错误的原因很简单:
Foo<T>::Bar中的T是一个非推导上下文,参见§14.8.2.5/5。正如其他人指出的那样,使函数成为朋友是可行的(因为生成的函数不是模板,所以不需要类型推导)。 -
@JamesKanze:“非推断上下文”是这两个问题的共同原因。考虑到给定
X,你可以为T解决Foo<T>::Bar = X始终是个问题。 -
@KerrekSB 这不是你能不能解决问题的问题,而是标准规定你不能尝试的事实。 (事实上,这个问题是可以解决的,但标准委员会认为当前的编译器技术很复杂。)
标签: c++ templates operator-overloading friend nested-class