Effective STL 39 Make predicates pure functions

来源:互联网 发布:音频美化软件 编辑:程序博客网 时间:2024/06/06 02:06

Even const member functions may access mutable data members, non-const local static objects, non-const class staic objects. non-const objects at namespace scope, and non-const global objects.

find_if never called p, it called only a copy of p;

tmplate<typename FwdIterator, typename Predicate>FwdIterator remove_if(FwdIterator begin, FwdIterator end, Predicate p) {    begin = find_if(begin, end, p);     if (begin == end) return begin;    else {        FwdIterator next = begin;        return remove_copy_if(++next, end, begin, p);    }}
class Predicate:    public unary_function<Widget, bool> {public:    Predicate() {}    bool operator()(const Widget&) const {        return ++timesCalled == 3;    }private:    static size_t timesCalled = 0;};