【发布时间】:2020-03-04 15:16:01
【问题描述】:
考虑以下将operator<< 重载为class A 的示例代码:
#include <iostream>
class A {
template <typename T>
friend A &operator<<(A &a, const T &t)
{
std::cout << t << std::endl;
return a;
}
friend A &operator<<(A &a, const std::string &t)
{
return operator<<<std::string>(a, t + "x");
}
};
我的意图是第二个运算符显式调用第一个。
但是,在 g++ 7.4 中,这会失败
在函数'A&运算符 错误:'操作员 返回运算符(a, t + "x");
^~
错误:“>”标记之前的预期主表达式
返回运算符(a, t + "x");
^
但是我不明白为什么这不应该编译。
【问题讨论】:
-
也许
return A::operator<< <std::string>(a, t + "x");?音符空间 -
@metablaster 失败并出现 错误:'operator
-
Clang has similar results
"error: use of undeclared 'operator<<'""error: expected '(' for function-style cast or type construction"。几乎就像它认为operator<<<std::string>是一个 type
标签: c++ g++ operator-overloading specifications