gpt4 book ai didi

c++ - 无法编译 bool 连接

转载 作者:搜寻专家 更新时间:2023-10-31 00:08:11 25 4
gpt4 key购买 nike

我的 Catch 测试框架中有以下简单表达式。理想情况下,如果测试失败,测试应该发出警告。

不幸的是,Catch 无法编译以下代码片段:

#define CATCH_CONFIG_MAIN
#include "catch2.hpp"

TEST_CASE("Simple") {
int a = 4;
int b = 1;
CHECK(a == 5 || b == 2);
}

Visual Studio 2015 发出以下错误:

error C2676: Binary operator "||": "const Catch::BinaryExpr<LhsT,const int &>" does not define this operator or a conversion for this operator of to suitable type

我希望是这样的:

4==5 || 1==2 => false || false => false

这对 Catch 是否可行,还是我必须使用额外的括号:

#define CATCH_CONFIG_MAIN
#include "catch2.hpp"

TEST_CASE("Simple") {
int a = 4;
int b = 1;
CHECK((a == 5 || b == 2));
}

最佳答案

如果您阅读 docs你会发现一个部分说:

CHECK( a == 2 || b == 1 ); This expression is too complex because of the || operator. If you want to check that one of several properties hold, you can put the expression into parenthesis (unlike with &&, expression decomposition into several CHECKs is not possible).

还要注意...

I would expect something like the following:

4==5 || 1==2 => false || false => false

...您的期望略有偏差。 || 是短路的,这意味着如果第一个操作数的计算结果为 false,则不会计算第二个操作数。

关于c++ - 无法编译 bool 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50150492/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com