At the doc page of boost::hana::always
I read that
always(x)
is a function such thatalways(x)(y...) == x
for any
y...
.
This makes me think that it shouldn't behave any differently than this lambda: [](auto const&...){ return false; }
.
However it does. For instance, the following code prints 11
, but if I change the third lambda to hana::always(false)
, then it prints 00
, revealing that always
is swallowing any argument.
#include <boost/hana/functional/always.hpp>
#include <boost/hana/functional/overload.hpp>
#include <iostream>
auto fun = boost::hana::overload(
[](int){ return true; },
[](double){ return true; },
[](auto const&...){ return false; }
);
int main() {
std::cout << fun(1) << fun(1.0) << std::endl;
}
- Is this expected?
- If so, why?
- Whether or not is expected, what causes this behavior?
By the way, I've just discovered boost::hana::overload_linearly
, which is not an alternative to boost::hana::overload
in this case (because as much as always
would not get all the calls, it would be the [](int){ return true; }
to be greedy), but it's good to know of it.
In fact,
always
has different overloads (as it handles reference as return value).so, with a simplified version:
then
Possible overloads are:
bool& my_always<bool>::operator ()(int&&)
#1const bool& my_always<bool>::operator ()(int&&) const
#2bool lambda::operator() (int) const
#3All are viable here, but
#1
is the best match (asfun
is notconst
(andint
is not better thanint&&
)).With
const
fun
, #3 would be the best match (#1 not viable, tie-breaker between #2 and #3 is template status).