简体   繁体   English

为什么BOOST_TEST((Iterator == Iterator))需要额外的括号?

[英]Why does BOOST_TEST((Iterator == Iterator)) require extra parentheses?

I am testing my implementation of binary search from binsearch.hpp : 我正在测试我从binsearch.hpp实现的二进制搜索:

template<typename Iterator, typename T>
Iterator binsearch(Iterator begin, Iterator end, const T &v) {
    if (std::distance(begin, end) == 0) {return end;}

    Iterator save = end;

    while (std::distance(begin, end) > 0) {
        Iterator mid = begin + std::distance(begin, end) / 2;

        if (*mid == v) {
            return mid;
        }

        if (v < *mid) {
            end = mid;
        } else {
            begin = mid + 1;
        }
    }

    return save;
}

with the following boost-driven unit test test_binsearch.cpp : 使用以下boost驱动单元测试test_binsearch.cpp

#define BOOST_TEST_MODULE test_binsearch
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>

#include "binsearch.hpp"

BOOST_AUTO_TEST_CASE(empty_0) {
    std::vector<int> xs = {};

    const auto result = binsearch(xs.begin(), xs.end(), 42);

    BOOST_TEST((result == xs.end()));
}

Unless I surround the comparison inside the BOOST_TEST with an extra pair of parentheses, I get a very cryptic and long compilation error that repeatedly tries to convert an iterator to a char, an error code and a few other types: 除非我用一对额外的括号包围BOOST_TEST内部的比较,否则我会得到一个非常神秘且长时间的编译错误,它会反复尝试将迭代器转换为char,错误代码和其他一些类型:

note: cannot convert 't' (type 'const __gnu_cxx::__normal_iterator std::vector >') to type 'char' 注意:无法将't'(类型'const __gnu_cxx :: __ normal_iterator std :: vector>')转换为'char'类型

  ostr << t; 

I got the clue to surround my comparison with extra parentheses from here . 从这里得到了用额外括号围绕我的比较的线索。 Why does it fail to compile without them? 为什么没有它们就无法编译?

From the documentation of BOOST_TEST: 从BOOST_TEST的文档

Complex statements 复杂的陈述

BOOST_TEST provides an enhanced reporting capability: additional details of the failing operands and operations are provided in the log , as shown on the example below: BOOST_TEST提供增强的报告功能: 日志中提供了故障操作数和操作的其他详细信息 ,如下例所示:

Example: BOOST_TEST enhanced reporting ( reduced version ) 示例:BOOST_TEST增强报告简化版

Code

 #define BOOST_TEST_MODULE boost_test_macro3 #include <boost/test/included/unit_test.hpp> BOOST_AUTO_TEST_CASE( mytest ) { int a = 13, b = 12; BOOST_TEST(a - 1 < b); } 

Output 产量

 > ./boost_test_macro3 --log_level=all Running 1 test case... Entering test module "boost_test_macro3" test.cpp(12): Entering test case "mytest" test.cpp(17): error: in "mytest": check a - 1 < b has failed [13 - 1 >= 12] ^^^^^^^^^^^^ !!! sic !!! ... 

Pay attention to the emphasized fragment of the test failure report. 注意测试失败报告的重点片段。 For those with moderate conduct of C++ it may seem like magic (newbies don't get surprised since they are not aware of the limitations of the language; experts know the secret or figure it out easily). 对于那些具有中等程度的C ++的人来说,它可能看起来像魔术(新手不会感到惊讶,因为他们不知道语言的局限性;专家知道这个秘密或很容易理解它)。

The documentation superficially explains the "magic" as follows: 该文档表面上解释了“魔术”如下:

BOOST_TEST parses the statement and constructs an expression out of it. BOOST_TEST 解析该语句并从中构造一个表达式。

However, the "magic" doesn't work with types that cannot be "printed to an std::ostream ". 但是,“魔术”不适用于无法“打印到std::ostream ”的类型。 Hence your compilation error. 因此您的编译错误。 Fortunately the "magic" (which is based on operator overloading) cannot override C++ rules of operator precedence. 幸运的是,“魔术”(基于运算符重载)不能覆盖运算符优先级的C ++规则。 By adding the extra pair of parentheses you define the territory where the "magic" cannot reach. 通过添加额外的括号,您可以定义“魔法”无法到达的区域。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM