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);
}
It const
std::function
may cause no const
operator()
.
Conclusion:
12
The same thing happens if I put a mutable
lambda, for example.test([x = 0]() mutable { ++x; });
How can it be?
Is it normal what a const std::function
mutable functor can wrap?
source
share