Consider the following example:
#include <iostream>
#include <functional>
struct A
{
int i;
void operator()()
{
std::cout << ++i;
}
};
void test(std::function<void()> const& fun)
{
fun();
}
int main() {
const std::function<void()> f{A{}};
test(f);
test(f);
}
Here, a const
std::function
is able to call a non-const
operator()
.
Output:
12
The same happens if I supply a mutable
lambda e.g. test([x = 0]() mutable { ++x; });
How can that be?
Is it normal that a const std::function
may wrap a mutable functor?