C++11 短路求值也会求值右侧操作数,即使它确定了左侧操作数的结果?

C++11 短路求值也会求值右侧操作数,即使它确定了左侧操作数的结果?,c++11,C++11,根据短路计算规则,仅当左侧操作数不确定结果时,才计算右侧操作数。 那为什么呢 即使左侧条件为真,似乎也要评估右侧条件 这是我的交换功能: void swap(string::iterator beg, string::iterator end) { std::cout << *end << std::endl; //to test if end points to the last element or one past the last element

根据短路计算规则,仅当左侧操作数不确定结果时,才计算右侧操作数。 那为什么呢

即使左侧条件为真,似乎也要评估右侧条件

这是我的交换功能:

void swap(string::iterator beg, string::iterator end)
{
    std::cout << *end << std::endl; //to test if end points to the last element or one past the last element
    char temp = *beg;
    *beg = *end;
    *end = temp;
}
void交换(string::iterator beg,string::iterator end)
{
标准::cout
即使左边的条件为真,为什么似乎也要评估右边的条件

因为
&&
运算符–当两个操作数都为true时,它返回true

只有左操作数为真不足以使整个表达式为真。
如果需要,请使用
运算符(
|

但是,当左操作数为false时,无论右操作数是什么,

整个表达式不能为true。那么,不计算右操作数。

为什么认为要计算右操作数?因为交换函数调用将递减的最后一个参数传递给它的第二个参数
void swap(string::iterator beg, string::iterator end)
{
    std::cout << *end << std::endl; //to test if end points to the last element or one past the last element
    char temp = *beg;
    *beg = *end;
    *end = temp;
}