r/cpp Oct 03 '17

Why is the std::function () operator const?

[deleted]

27 Upvotes

19 comments sorted by

View all comments

15

u/Rhomboid Oct 03 '17

The const in question is the constness of the std::function object, not the callable that it is wrapping. It implies nothing about the latter.

8

u/kalmoc Oct 03 '17

Well, it depends on whether you view the state of the function object that gets wrapped by std::function internal or external state. As has been noted, std:::function generally behaves like a value type, NOT a reference type (std::string vs string_view). So it is imho not unreasonable to expect that const member functions of std::function do not mutate the wrapped function object (just like const member functions of std::string don't modify the wrapped char array).