c++elision

Can a call to an STL method be elided?


Consider the following situation:

#include <algorithm>
#include <vector>

class T {/*...*/};
bool predicateA(const T& t) {/*...*/}
bool predicateB(T& t) {/*...*/}

bool evaluate(std::vector<T>& vA, std::vector<T>& vB)
{
  return std::ranges::count_if(vA, predicateA) + std::ranges::count_if(vB, predicateB);
}

If std::ranges::count_if(vA, predicateA) returns a positive value, then evaluate will return true no matter what the other call to count_if returns. Would the compiler be required to evaluate the second call to count_if anyway? Notice that in this particular situation eliding that call might change the behavior of the program, since predicateB is allowed to have side effects.


Solution

  • No, certainly not. The only short-circuit evaluation is that defined by the language like for && and ||.