【问题标题】:Overload output stream operator of a template class outside of the template在模板外重载模板类的输出流运算符
【发布时间】:2013-05-29 12:35:34
【问题描述】:

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

在模板类中实现就可以了:

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

但是当我尝试在模板类之外进行时:

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)

谁能举一个简单的例子说明如何在模板类之外定义输出流运算符&lt;&lt;

在我在这里找到的相关帖子中,每个人都在模板类中执行此操作。

【问题讨论】:

  • 停下来想一想:“真的需要friend吗?”
  • @BoBTFish 我不确定答案。我曾经在我的非模板类中将其声明为朋友。
  • 它是否使用任何private 成员数据或函数?有必要吗?

标签: c++ templates operator-overloading


【解决方案1】:

谁能给出一个简单的例子,说明输出流操作符

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

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

在我在这里找到的相关帖子中,每个人都在模板类中执行此操作。

我会这样做;它会简单得多。或者,更好的是,我会根据公共接口实现输出,假设它可以充分访问容器的内容。那么你就不需要朋友声明,所以也不需要前向声明。

【讨论】:

  • 非常感谢迈克。这就是我想要的。我明白了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-26
  • 2012-02-07
  • 2016-10-15
  • 1970-01-01
  • 2013-02-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多