The associativity of the == operator in C ++

I have this piece of code in C ++:

std::vector<int> v1;
std::vector<int> v2;
...
if (v1.insert(v1.end(), v2.begin(), v2.end()) == v1.end())
{
    return 0;
}

Which expression will be evaluated first? When debugging, the right side of the "==" operator is processed first, is this the correct behavior?

+4
source share
2 answers

This has nothing to do with associativity (which comes into play in type expressions a == b == c). What are you asking about is the evaluation order of the operands of the operator. With some explicit exceptions, this is not intentionally indicated in C ++. This means that there is no guarantee there will first be evaluated aor bin a == b.

Exceptions (for which the evaluation procedure is guaranteed):

  • ( ).
  • || && ( , ).
  • , .
  • ?: , .

, &&, || , , . .

+12

operator ==, . , , , . , .

+4

Source: https://habr.com/ru/post/1672005/


All Articles