Обьяснял сегодня человеку почему нельзя определить локальный предикат.
Хм, сам задумался, а почему, собственно, нельзя? B результате обьявил.
#include <stdio.h>
#include <algorithm>
template < class T >
struct local_predicat_t {
typedef T value_type;
struct worker {
const local_predicat_t<T>& predicate_;
worker(const local_predicat_t<T>& p) : predicate_(p) {}
bool operator()(T i) const { return predicate_(i); }
};
virtual bool operator()(T i) const = 0;
const worker operator~() const { return worker(*this); }
};
int main(void)
{
struct predicat_t : local_predicat_t<const int&> {
int a_;
predicat_t(int x) : a_(x) {}
bool operator()(value_type i) const { printf("%d:%d\n",i,a_); return false;}
};
const int a[4] = {0,1,2,3};
std::for_each(a,a+4,~predicat_t(1));
return 0;
}
P.S.
В любой шутке есть доля шутки.