【问题标题】:Operator overload not found when used in a lambda within a namespace在命名空间内的 lambda 中使用时未找到运算符重载
【发布时间】:2018-04-06 09:12:46
【问题描述】:

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

Clang 中的错误消息:

错误:二进制表达式的操作数无效(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 does not work for typedefs and using type aliases。这里有什么问题?为什么在上述情况下编译器找不到operator+(MyType, const MyType &amp;)

以下所有替代方案都可以编译:

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

【问题讨论】:

  • @user463035818 我要使用的operator+MyType,是调用Func实例的结果。代码是lhs() + rhs(),而不是lhs + rhs。我在这里错过了什么吗?
  • 呃,对不起,我错过了一些东西。请在问题中包含错误消息
  • @JiveDadson 是的:“这里有什么问题?”,意思是为什么这不能编译?为什么在上述情况下编译器找不到operator+(MyType, const MyType &amp;)?我更新了问题以进一步澄清这一点。
  • 这里有一个有趣的花絮:This 编译,但 adding an unrelated operator+ 杀死它...
  • @JiveDadson 他显然想知道是什么使它非法。而且我认为示例大小没有问题,对于他分享的所有观察结果来说,它是完整的、可验证的和最小的。

标签: c++ operator-overloading using


【解决方案1】:

您正在隐藏全局命名空间operator+,因为您在当前范围内定义了operator+。非限定名称查找一直移动到封闭的命名空间,直到找到至少一个声明。所以因为在当前命名空间中有一个operator+,不管它的参数列表如何,名称查找都不会继续在全局命名空间中进行搜索。有关合格查找的详细说明,请参阅here。相关位在顶部。

对于非限定名称,即未出现在范围解析运算符 :: 右侧的名称,名称查找会按如下所述检查范围,直到找到至少一个任何类型的声明,此时查找停止,不再检查范围。

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

在非限定名称查找中显式包含 ::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

【讨论】:

    【解决方案2】:

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

    cppreference

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

    考虑以下代码:

    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+是通过非限定名称查找找到的,只要找到名称就会停止。在namespace ns中声明operator+时,首先找到该操作符,不会搜索全局命名空间中的操作符。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-09-15
      • 2011-12-03
      • 1970-01-01
      • 2017-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多