不能在 Catch 测试中使用重载的比较运算符

Som*_*ude 5 c++ unit-testing operator-overloading catch2

我有一个使用 Catch 2.11.1 的简​​单单元测试:

#define CATCH_CONFIG_MAIN
#include "catch.hpp"
#include <utility>
#include <any>

namespace A::B
{
    namespace C
    {
        struct S
        {
        };
    }

    using type = std::pair<C::S, std::any>;
}

inline bool operator==(A::B::type const&, A::B::type const&)
{
    return true;
}

TEST_CASE("test", "[test]")
{
    auto t1 = std::make_pair(A::B::C::S(), std::any());
    auto t2 = std::make_pair(A::B::C::S(), std::any());

    REQUIRE(t1 == t2);
}
Run Code Online (Sandbox Code Playgroud)

上面的简单程序会产生以下错误:

$ g++ -Wall -Wextra -Wpedantic test-single.cpp -std=c++17
In file included from /usr/include/c++/9/bits/stl_algobase.h:64,
                 from /usr/include/c++/9/bits/char_traits.h:39,
                 from /usr/include/c++/9/string:40,
                 from catch.hpp:457,
                 from test-single.cpp:2:
/usr/include/c++/9/bits/stl_pair.h: In instantiation of ‘constexpr bool std::operator==(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&) [with _T1 = A::B::C::S; _T2 = std::any]’:
catch.hpp:2289:98:   required from ‘bool Catch::compareEqual(const LhsT&, const RhsT&) [with LhsT = std::pair<A::B::C::S, std::any>; RhsT = std::pair<A::B::C::S, std::any>]’
catch.hpp:2318:34:   required from ‘const Catch::BinaryExpr<LhsT, const RhsT&> Catch::ExprLhs<LhsT>::operator==(const RhsT&) [with RhsT = std::pair<A::B::C::S, std::any>; LhsT = const std::pair<A::B::C::S, std::any>&]’
test-single.cpp:28:5:   required from here
/usr/include/c++/9/bits/stl_pair.h:449:24: error: no match for ‘operator==’ (operand types are ‘const A::B::C::S’ and ‘const A::B::C::S’)
  449 |     { return __x.first == __y.first && __x.second == __y.second; }
      |              ~~~~~~~~~~^~~~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)

[在此之后还有更多的消息......]

错误消息的关键部分是这一行:

/usr/include/c++/9/bits/stl_pair.h: In instantiation of ‘constexpr bool std::operator==(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&) [with _T1 = A::B::C::S; _T2 = std::any]’:
Run Code Online (Sandbox Code Playgroud)

从错误消息中可以清楚地看出,它是正在调用的标准std::operator==函数std::pair,而不是我的重载operator==函数。

如果我不在CatchREQUIRE宏中进行比较,则它有效:

auto result = t1 == t2;  // Invokes my overloaded comparison operator
REQUIRE(result);
Run Code Online (Sandbox Code Playgroud)

现在这是 Catch 的问题,还是我的操作符函数的问题?


注意:我正在使用最新版本的 GCC 9.2 在 Debian SID 上构建

$ g++ --version
g++ (Debian 9.2.1-23) 9.2.1 20200110
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Run Code Online (Sandbox Code Playgroud)

Max*_*hof 6

请注意,即使使用 Lightness 建议的括号,您显示的代码也异常脆弱。

由于宏内部的依赖名称查找,我猜您最初处于仅 ADL 领域(请参阅https://en.cppreference.com/w/cpp/language/adl的最后一个注释),并且您的代码显然不是ADL 可行。添加括号使整个事情只是一个不合格的查找,而不是仅 ADL(再次,猜测)。在这种情况下,unqualified lookup 的非 ADL 部分可以节省您的时间,但它会因完全不相关的代码更改而分崩离析。

考虑这段代码而不是 ,TEST_CASE使用括号大概可以归结为:

namespace test
{
    bool foo()
    {
        auto t1 = std::make_pair(A::B::C::S(), std::any());
        auto t2 = std::make_pair(A::B::C::S(), std::any());

        return t1 == t2;
    }
}
Run Code Online (Sandbox Code Playgroud)

这会编译并按预期工作:https : //godbolt.org/z/HiuWWy

现在operator==在您的 globaloperator==和之间添加一个完全无关的t1 == t2

namespace test
{
    struct X{};
    bool operator==(X, X);

    bool foo()
    {
        auto t1 = std::make_pair(A::B::C::S(), std::any());
        auto t2 = std::make_pair(A::B::C::S(), std::any());

        return t1 == t2;
    }
}
Run Code Online (Sandbox Code Playgroud)

你出去算了:https : //godbolt.org/z/BUQC9Y

operator==全局命名空间中没有找到,因为不合格的名称查找(非ADL部分)在停止第一个具有封闭范围的任何 operator==。由于这没有找到任何有用的东西,它退回到使用内置std::pair比较运算符(通过 ADL 找到),这不起作用。

只需将运算符重载放在它们操作的对象的命名空间中。根据推论,不要为来自std(或其他不允许接触的命名空间)的设施重载运算符。


从评论中添加:

该标准目前还指出,模板参数的命名空间被认为是,所以把operator==namespace C将工作(因为的std ::对的第一个模板参数来自那里):https://godbolt.org/z/eV8Joj

但是,1. 这与您的类型别名不太吻合,2. 有一些动作可以使 ADL 不那么狂野,我已经看到讨论摆脱“考虑模板参数的命名空间”。见http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0934r0.pdf

我们到底为什么要研究模板参数的命名空间?那里的任何东西都不可能是类型接口的一部分,除非模板参数也是基类或其他东西。——赫伯·萨特

我不知道这篇论文今天处于什么位置,但我会避免在新代码中依赖这种 ADL。

  • 根据您的回答,我将“type”转换为新结构,并将比较运算符移至“A::B”命名空间中。现在效果很好。 (2认同)

Lig*_*ica 5

扩展操作数以提供良好诊断输出的魔法有时会失败。

解决方法是使用一些括号禁用它:

REQUIRE((t1 == t2));
Run Code Online (Sandbox Code Playgroud)

这实际上与使用变量的解决方法相同。

文档在更复杂的表达式的上下文中提到了这个问题。究竟为什么在您的情况下触发这种情况我不确定,但是从堆栈跟踪中注意您operator==实际上没有被调用,而是Catch::BinaryExpr::operator==and Catch::compareEqual,它似乎无法访问(或以其他方式选择不使用)您的实现. 无论哪种方式,解决方案是禁用上述分解机制。