在模板外部重载模板类的输出流运算符 [英] Overload output stream operator of a template class outside of the template

查看:151
本文介绍了在模板外部重载模板类的输出流运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在模板类定义之外重载输出流运算符<

I want to overload the output stream operator << outside the template class definition.

它在模板类内是确定的:

Implementing it inside the template class is ok:

template
<typename T,int _MaxSize=10,template <class C> class Policy=NoCheck,typename Container=std::vector<T>>
class MyContainer : public Policy<T>
{
public:
  MyContainer():p(_MaxSize){};
  std::ostream& operator<<(MyContainer<T,_MaxSize,Policy,Container>& obj){ };
private:
  Container p;
};

但是当我试图在模板类之外做:

But when I tried to do it outside the template class:

template
<typename T,int _MaxSize=10,template <class C> class Policy=NoCheck,typename Container=std::vector<T>>
class MyContainer : public Policy<T>
{
public:
  MyContainer():p(_MaxSize){};
  friend std::ostream& operator<<(std::ostream& out,MyContainer<T,_MaxSize,Policy,Container> obj);
private:
  Container p;
};

template
<typename T,int _MaxSize,template <class C> class Policy,typename Container>
std::ostream& operator<<(std::ostream& out,MyContainer<T,_MaxSize,Policy,Container> obj)
{
};

编译器抱怨:

warning: friend declaration ‘std::ostream& operator<<(std::ostream&, MyContainer<T, _MaxSize, Policy, Container>)’ declares a non-template function [-Wnon-template-friend]
tempstruct.cc:39:97: note: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here)

任何人都可以给出一个简单的例子,如何在模板类外定义输出流操作符< / strong>

Can anybody give a simple example on how can the output stream operator << defined outside a template class?

在我发现的相关帖子中,每个人都在模板类中做。

In the related posts that I found here everyone do it inside the template class.

推荐答案


任何人都可以给出一个关于输出流操作符<<在模板类外部定义?

Can anybody give a simple example on how can the output stream operator << defined outside a template class?

不,因为它不简单。我可以给一个复杂的例子:

No, because it's not simple. I can give a complicated example:

// Declare the class template, because we need it to declare the operator
template <typename,int,template <class C> class,typename> class MyContainer;

// Declare the operator, because (as the error says) templates must be declared
// in the namespace before you can declare them friends. At this point, we can't
// define the operator, since the class template is incomplete.
template 
<typename T,int _MaxSize,template <class C> class Policy,typename Container>
std::ostream& operator<<(std::ostream&,MyContainer<T,_MaxSize,Policy,Container>);

// Define the class template
template
<typename T,int _MaxSize=10,template <class C> class Policy=NoCheck,typename Container=std::vector<T>>
class MyContainer : public Policy<T>
{
public:
  MyContainer():p(_MaxSize){};

  // Include <> to indicate that this is the template
  friend std::ostream& operator<< <>(std::ostream& out,MyContainer<T,_MaxSize,Policy,Container> obj);
private:
  Container p;
};

// Finally define the operator
template
<typename T,int _MaxSize,template <class C> class Policy,typename Container>
std::ostream& operator<<(std::ostream& out,MyContainer<T,_MaxSize,Policy,Container> obj)
{
  // print some stuff
};




在我发现的相关帖子中,每个人都在模板中类。

In the related posts that I found here everyone do it inside the template class.

它会更简单。或者,更好的是,我将在公共接口方面实现输出,假设它提供了足够的访问容器的内容。那么你不需要朋友声明,因此也不需要前向声明。

I'd do that; it would be much simpler. Or, better still, I'd implement output in terms of the public interface, assuming it gives sufficient access to the container's contents. Then you wouldn't need the friend declaration, so wouldn't need the forward declarations either.

这篇关于在模板外部重载模板类的输出流运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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