运算符<<<(ostream& amp; X)对于嵌套在类模板中的类X [英] operator<<(ostream&, X) for class X nested in a class template

查看:378
本文介绍了运算符<<<(ostream& amp; X)对于嵌套在类模板中的类X的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个编译和工作原理(非嵌套模板):

  #include< iostream> 

template< typename T> Z类;

template< typename T>
std :: ostream&运算符<< (std :: ostream& os,const Z< T>){
return(os<<Z);
}

template< typename T> class Z {
friend std :: ostream&运算符<< <> (std :: ostream& os,const Z&);
};

int main(){
Z< int> z;
std :: cout<< z < std :: endl;
}

这不能编译(gcc 4.4和gcc 4.6,和0x模式):

  #include< iostream> 

template< typename T> Z类;

template< typename T>
std :: ostream&运算符<< (std :: ostream& os,const typename Z T :: ZZ&){
return(os<ZZ!
}

template< typename T> class Z {
public:
class ZZ {
friend std :: ostream&运算符<< <> (std :: ostream& os,const ZZ&);
};
};


int main(){
Z< int> :: ZZ zz;
std :: cout<< zz < std :: endl;
}

错误消息如下所示:

 错误:template-id'operator< <>'for'std :: ostream& <<<<(std :: ostream& 
const Z< int> :: ZZ&)'不匹配任何模板声明
error: :: cout<< zz'

在0x模式下,第二个错误消息不同,但含义是一样的。 / p>

我可以做我想做的吗?



EDIT 这里有一个非推导上下文的实例,它解释了错误消息。然而,问题仍然是:我可以为嵌套在类模板中的类有一个有效的操作符<<吗?

解决方案

Matthieu很好地解释了这个问题,但是在这种情况下可以使用一个简单的解决方法。
可以使用friend声明在类中实现friend函数:

  #include< iostream> 

template< typename T> class Z {
public:
class ZZ {
friend std :: ostream&运算符<< (std :: ostream& os,const ZZ&){
return os< ZZ!;
}
};
};


int main(){
Z< int> :: ZZ zz;
std :: cout<< zz < std :: endl;
}


This one compiles and works like it should (non-nested template):

#include <iostream>

template<typename T> class Z;

template <typename T> 
std::ostream& operator<< (std::ostream& os, const Z<T>&) {
    return (os << "Z");
}

template<typename T> class Z {
    friend std::ostream& operator<< <> (std::ostream& os, const Z&);
};

int main () {
    Z<int> z;
    std::cout << z << std::endl;
}

This one doesn't compile (gcc 4.4 and gcc 4.6, in both 03 and 0x mode):

#include <iostream>

template<typename T> class Z;

template<typename T> 
std::ostream& operator<< (std::ostream& os, const typename Z<T>::ZZ&) {
    return (os << "ZZ!");
}

template <typename T> class Z {
  public:
    class ZZ {
        friend std::ostream& operator<< <> (std::ostream& os, const ZZ&);
    };
};


int main () {
    Z<int>::ZZ zz;
    std::cout << zz << std::endl;
}

The error message looks like this:

error: template-id ‘operator<< <>’ for ‘std::ostream& operator<<(std::ostream&,
const Z<int>::ZZ&)’ does not match any template declaration
error: no match for ‘operator<<’ in ‘std::cout << zz’

In the 0x mode the second error message is different, but the meaning is the same.

Is it possible to do what I want to do?

EDIT Apparently, there's an instance of non-deduced context here, which explains the error messages. The question, however, still stands: can I have a working operator<< for a class nested in a class template?

解决方案

Matthieu explained the problem very well, but a simple work-around can be used in this case. You can implement the friend function inside the class with the friend declaration:

#include <iostream>

template <typename T> class Z {
public:
  class ZZ {
    friend std::ostream& operator<< (std::ostream& os, const ZZ&) {
      return os << "ZZ!";
    }
  };
};


int main () {
  Z<int>::ZZ zz;
  std::cout << zz << std::endl;
}

这篇关于运算符&lt;&lt;&lt;(ostream&amp; amp; X)对于嵌套在类模板中的类X的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆