如何重载运算符<<输出模板内定义的向量?
How to overload operator<< to output a vector, defined inside a template?
下面的示例是我遇到的模板问题的提炼版本 - 请参阅下面的编译错误。
#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
相关文章:
- 在提升multi_index容器中,是否定义了"default index"?
- #定义c-预处理器常量..我做错了什么
- 用C++中的一个变量定义一个常量
- 部分定义/别名模板模板参数
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- #为""定义宏;静态";针对不同的上下文
- 如何确保C++函数在定义之前声明(如override关键字)
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 当类在C++中定义时,有什么方法可以"register"类吗?
- 在命名空间中定义函数还是限定函数
- 此代码是否违反一个定义规则
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 编译C++时未定义的引用
- 不同翻译单元中不可重载的非内联函数定义
- 为什么在定义函数之前先声明它
- 如何过载<<用于YAML::Emitter的运算符,以序列化包含另一个自定义类的向量的自定义类
- 1<<31产生误差;'<<'表达式未定义”;
- 当运算符<存在时,为什么要定义 LT?
- 重载& lt; & lt;用于打印自定义异常的操作符
- & lt; & lt;使用继承和模板定义