Suppose I have a callable type like so:
struct mutable_callable
{
int my_mutable = 0;
int operator()() { // Not const
return my_mutable++;
}
};
Note that mutable_callable
has a non-const operator()
that modifies a member variable.....
Now suppose I create a std::function
out of my type:
std::function<int()> foo = mutable_callable{};
Now I can do this:
void invoke(std::function<int()> const& z)
{
z();
}
int main()
{
invoke(foo); // foo changed.....oops
}
Now as far as I can tell std::function
s operator()
is const
as per:
https://en.cppreference.com/w/cpp/utility/functional/function/operator()
So my gut feeling is that you shouldn't be able to do this.....
But then looking at: https://en.cppreference.com/w/cpp/utility/functional/function/function
This doesn't seem to put any constraints on whether or not the callable type has a constant operator()
......
So my question is this: I am correct in assuming that std::function<int()> const&
is essentially the same thing as std::function<int()>&
that is there is no actually difference between the behavior of the two......and if that is the case why is it not const
correct?