在命名空间内的 lambda 中使用时未找到运算符重载

Operator overload not found when used in a lambda within a namespace

本文关键字:重载 运算符 命名空间 lambda      更新时间:2023-10-16

以下内容无法编译(使用 Clang 5.0.0/gcc 7.3,std: C++11):

叮当中的错误消息:

错误:二进制表达式的操作数无效(std::vector<double, std::allocator<double> >std::vector<double, std::allocator<double>>)

#include <functional>
#include <vector>
namespace ns{
using MyType = std::vector<double>;
} // namespace ns
using ns::MyType;
MyType& operator+=( MyType& lhs, const MyType& rhs) {
for (int i = 0; i < lhs.size(); ++i) {
lhs[i] = lhs[i] + rhs[i];
}
return lhs;
}
MyType operator+( MyType lhs, const MyType& rhs) {
lhs += rhs;
return lhs;
}
namespace ns{
using Func = std::function<MyType()>;
Func operator+(
const Func &lhs, const Func &rhs) {
return [lhs, rhs]() {
auto y = lhs() + rhs(); // <-- error in this line
return y;
};
}
} // namespace ns

编译器找不到operator+的正确重载。我不明白为什么。我在命名空间之外定义运算符的原因是 ADL 不适用于 typedefs 和使用类型别名。这里有什么问题?为什么编译器在上述情况下找不到operator+(MyType, const MyType &)

可以编译以下所有替代项:

namespace ns {
MyType a, b;
auto c = a + b; // compiles
MyType f() {
MyType a_, b_;
return a_ + b_; // compiles
};
Func operator+(
const Func &lhs, const Func &rhs) {
return [lhs, rhs]() {
auto x = lhs();
x += rhs(); // <-- compiles; operator+= instead of operator+
return x;
};
}
} // namespace ns
Func operator+(
const Func &lhs, const Func &rhs) {
return [lhs, rhs]() {
auto y = lhs() + rhs(); // <-- no error if not in namespace ns
return y;
};
}

您隐藏了全局命名空间operator+,因为您已在当前作用域中定义了operator+。非限定名称查找将一直移动到封闭命名空间,直到找到至少一个声明。因此,由于当前命名空间中存在operator+,因此无论其参数列表如何,名称查找都不会继续在全局命名空间中进行搜索。有关合格查找的详细说明,请参阅此处。相关位位于顶部。

对于非限定名称(即未显示在作用域解析运算符 :: 右侧的名称),名称查找将按如下所述检查作用域,直到找到至少一个任何类型的声明,此时查找将停止,不再检查作用域。

请注意这两个示例的工作原理。仅显示更改的段,其余代码必须仍然可见才能编译。

在非限定名称查找中显式包含 ::operator++

namespace ns {
using Func = std::function<MyType()>;
using ::operator+;
Func operator+(
const Func &lhs, const Func &rhs) {
return [lhs, rhs]() {
auto y = lhs() + rhs();
return y;
};
}
} // namespace ns

确保没有函数隐藏全局operator+

namespace ns {
using Func = std::function<MyType()>;
Func func(
const Func &lhs, const Func &rhs) {
return [lhs, rhs]() {
auto y = lhs() + rhs();
return y;
};
}
} // namespace ns

这是因为 ADL 不会自动搜索封闭命名空间,除非它是内联命名空间:

CPP 首选项

如果关联的类和命名空间集中的任何命名空间是内联命名空间,则其封闭命名空间也会添加到该集中。

请考虑以下代码:

namespace N {
namespace M {
struct foo {};
}
void operator+(M::foo, M::foo) {}
}

int main()
{
N::M::foo f;
f + f; // Error, enclosing namespace N won't be searched.
}

同样,在您的情况下,关联的命名空间std,ADL 不会考虑封闭的全局命名空间。

实际上,代码中的operator+是通过非限定名称查找找到的,只要找到名称,该查找就会停止。当operator+namespace ns中声明时,首先找到此运算符,并且不会搜索全局命名空间中的运算符。