Search code examples
c++boostboost-hana

Boost Hana any_of?


So I tried to use Boost Hana's any_of method, but unlike the description in the implementation, it still calls the element after the element that first satisfied the predicate. Is this a know bug ?

Here is an MCVE:

#include <iostream>
#include <boost/hana.hpp>

int main() {
    auto t = boost::hana::tuple_t<int, double, float>;
    boost::hana::any_of(t, [](auto) { std::cout << "Called\n"; return true; });
}

Output:

Called
Called

Solution

  • This is a bug; thanks for finding it. The predicate was always evaluated one more time than strictly necessary. The bug is fixed by this commit, which will make its way into Boost 1.64.0.

    That being said, Hana's documentation specifically forbids you from relying on this (and also having side effects in the functions you send to algorithms): http://boostorg.github.io/hana/#tutorial-algorithms-effects. So, while I consider it a bug from a performance perspective, it is not a bug strictly speaking since it does not break the contract given to you by the library.

    The reason why I can't make this part of the contract of the function is that it might prevent some implementation strategies from being valid, and I want to keep this liberty. I would consider it more seriously if the use case was compelling, but it does not seem to be.