我惊讶地发现这段代码编译:
#include <functional> struct Callable { void operator() () { count++; } void operator() () const = delete; int count = 0; }; int main() { const Callable counter; // counter(); //error: use of deleted function 'void Callable::operator()() const' std::function<void(void)> f = counter; f(); const auto cf = f; cf(); }
https://wandbox.org/permlink/FH3PoiYewklxmiXl
这将调用Callable的非const调用操作符.相比之下,如果你做const auto cf = counter; CF();然后它按预期出错.那么,为什么const正确性似乎没有跟着std :: function?