如何重载运算符<<输出模板内定义的向量?

How to overload operator<< to output a vector, defined inside a template?

本文关键字:lt 定义 向量 输出 重载 运算符 何重载      更新时间:2023-10-16

下面的示例是我遇到的模板问题的提炼版本 - 请参阅下面的编译错误。

#include <iostream>
#include <vector>
template <class T> struct S
{
  typedef std::vector<T> SVec;
};
template <class T> std::ostream& operator<<(std::ostream& OS, const S<T>::SVec& X)
{
  for (const auto& e: X) OS << e << ' ';
  return OS;
}
int main()
{
  S<int>::SVec v;
  std::cout << v << std::endl;
}

编译器输出:

g++ -g -Wall -O4 -std=c++11 -c tc041.cpp
tc041.cpp:22:69: error: need ‘typename’ before ‘S<T>::SVec’ because ‘S<T>’ is a dependent scope
 template <class T> std::ostream& operator<<(std::ostream& OS, const S<T>::SVec& X)

依此类推 - 数百行。我的编译器 - g++ 5.2.1,操作系统 - Xubuntu 4.2.0。

如何使这个运算符正确编译?

有两个问题:

  • 运算符声明中缺少typename关键字。
  • 模板类型推断在main中失败。

template <class T>
std::ostream& operator<<(std::ostream& OS, const typename S<T>::SVec& X)

typename关键字应放在S<T>::SVec 之前。由于语言规则,编译器不知道SVec是成员类型还是其他类型。typename关键字是告诉编译器它是一个类型。

有关语言规则的详细信息,您可以参考: http://en.cppreference.com/w/cpp/language/dependent_name


(注意:我实际上不太确定,因为这种情况对我来说也是新的。

让我们做一个实验。与其使用运算符语法调用operator<<,不如使用函数语法调用它。

operator<< (std::cout, v);

好吧,它仍然失败。

如果我们使用显式模板参数调用它怎么办?

operator<< <int> (std::cout, v);

它有效!好吧,实际上,可能会起作用。我发现某些标准库标头可能包含干扰它的内容。但让我们把它放在一边。

此实验演示的问题是模板参数推导失败。无法推断class-template-name<T>::member-type形式的参数类型。

详细信息:http://en.cppreference.com/w/cpp/language/template_argument_deduction