Dmitry J
Dmitry J

Reputation: 927

C++: What is semantics of std::function constness?

Reference says that std::function::operator() is const. What this constness applies to? If std::function is a wrapper around callable object, does constness applies to this object (meaning its operator() should be const too?).

Overall, what does it mean for std::function to be const for different kinds of targets (lambda, function pointer, object, member function, etc)?

Upvotes: 2

Views: 73

Answers (1)

Potatoswatter
Potatoswatter

Reputation: 137870

It is a bug. Nothing is actually const as the target object is called from a non-const reference, per the specification.

A future revision of the standard library will remove const from the call operator. Users will be able to specifically request a properly const call using specializations such as std::function<return_type(arg_type) const>.

See standard proposal P0045. (Disclosure: I'm the author.)

Upvotes: 3

Related Questions