简体   繁体   English

在条件外使用短路运算符是否合法?

[英]Is it Legal to Use Short Circuit Operators Outside a Conditional?

The following is a questionable minimal, complete, verifiable example . 以下是一个可疑的最小,完整,可验证的示例 This is not a question about how to improve this code. 这不是关于如何改进此代码的问题。 What I do want to know is whether the standard condones the use of short circuit operators outside a conditional, as is demonstrated in main . 我所想知道的是标准是否纵容外有条件使用短路运营商,足以证明在main

enum weekday {
    SUNDAY,
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    SATURDAY,
    WEEKDAY_SIZE
};

bool getWeekday(int index, weekday& result) {
    result = static_cast<weekday>(index);

    return index >= 0 && index < static_cast<int>(WEEKDAY_SIZE);
}

bool getName(weekday& index, string& result) {
    switch (static_cast<weekday>(index)) {
    case SUNDAY:
        result = "Sunday";
        break;
    case MONDAY:
        result = "Monday";
        break;
    case TUESDAY:
        result = "Tuesday";
        break;
    case WEDNESDAY:
        result = "Wednesday";
        break;
    case THURSDAY:
        result = "Thursday";
        break;
    case FRIDAY:
        result = "Friday";
        break;
    case SATURDAY:
        result = "Saturday";
        break;
    default:
        assert("Short Circut Failed");
        return false;
    }
    return true;
}

int main() {
    const int index = 0;
    weekday Weekday;
    string Name;

    getWeekday(index, Weekday) && getName(Weekday, Name);

    cout << Name << endl;
}

This works for both Visual Studio 2015 and gcc 5.1 without asserting. 这适用于Visual Studio 2015和gcc 5.1,无需断言。

It's not the job of the standard to condone coding styles. 宽恕编码风格不是标准的工作。

There's nothing wrong with your writing getWeekday(index, Weekday) && getName(Weekday, Name); 你的写作getWeekday(index, Weekday) && getName(Weekday, Name);没有错getWeekday(index, Weekday) && getName(Weekday, Name);

A reader of your code will know that getName(Weekday, Name) will not be called if getWeekday(index, Weekday) evaluates to false . 如果getWeekday(index, Weekday)计算结果为false getWeekday(index, Weekday)则代码的读者将知道不会调用getName(Weekday, Name)

From the C++14 standard , section 5.14: 从C ++ 14标准 ,第5.14节:

1 The && operator groups left-to-right. 1 &&运算符从左到右分组。 The operands are both contextually converted to bool (Clause 4 ). 操作数都在上下文中转换为bool(第4条)。 The result is true if both operands are true and false otherwise. 如果两个操作数都为真,则结果为true,否则为false。 Unlike & , && guarantees left-to-right evaluation: the second operand is not evaluated if the first operand is false . 与&不同, &&保证从左到右的评估:如果第一个操作数为假,则不评估第二个操作数

2 The result is a bool . 2结果是一个布尔。 If the second expression is evaluated, every value computation and side effect associated with the first expression is sequenced before every value computation and side effect associated with the second expression. 如果评估第二表达式,则在与第二表达式相关联的每个值计算和副作用之前,对与第一表达式相关联的每个值计算和副作用进行排序。

The standard says nothing regarding the context of where && is used. 该标准没有说明使用何处&&的背景。 If the left hand side evaluates to false, the right hand side is not evaluated. 如果左侧评估为假,则不评估右侧。

In this context, the result of the expression is thrown away, similarly to if you did this: 在这种情况下,表达式的结果将被抛弃,类似于你这样做:

1;

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

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