pinto
pinto

Reputation: 919

Boost lambda bewilderment

Why is callback called once only?

bool callback()
{
    static bool res = false;
    res = !res;
    return res;
}

int main(int argc, char* argv[])
{
    vector<int> x(10);

    bool result=false;
    for_each(x.begin(),x.end(),var(result)=var(result)||bind(callback));

    return 0;
}

Upvotes: 3

Views: 417

Answers (2)

Rob Kennedy
Rob Kennedy

Reputation: 163357

In your particular example, Boost.Lambda doesn't really gain you anything. Get rid of the lambda parts, and maybe you'll see more clearly what's going on:

for (int i = 0; i < 10; ++i)
  result = result || callback();

This still relies on you to know that the || operator is short-circuited, as Daniel explained.

Upvotes: 1

Daniel LeCheminant
Daniel LeCheminant

Reputation: 51131

The || expression short circuits after the first time bind returns true.

The first time you evaluate

result = result || bind(...)  // result is false at this point

bind is called, because that's the only way to determine the value of false || bind(...). Because bind(...) returns true, result is set to true.

Every other time you say

result = result || bind(...)  // result is true at this point

... the bind(...) expression isn't evaluated, because it doesn't matter what it returns; the expression true || anything is always true, and the || expression short circuits.

One way to ensure that bind is always called would be to move it to the left side of the ||, or change the || to an &&, depending on what you are trying to accomplish with result.

Upvotes: 8

Related Questions