#include
#include
#include
#include
template
class if_functor{
CondT cond;
FunctorT functor;
public:
if_functor(CondT cond_,FunctorT functor_):cond(cond_),functor(functor_),else_(*this){}
template
void operator ()(const T& value)const{
if(cond(value)){
functor(value);
}
}
template
class else_functor{
CondT cond;
FunctorT functor;
FunctorT2 functor2;
public:
else_functor(CondT cond_,FunctorT functor_,FunctorT2 functor2_):cond(cond_),functor(functor_),functor2(functor2_){}
template
void operator()(const T& value){
cond(value) ? functor(value) : functor2(value) ;
}
};
class else_functor_factory{
const if_functor&parent;
public:
else_functor_factory(const if_functor&parent_):parent(parent_){}
template
else_functor operator[](FunctorT2 functor){
return else_functor(parent.cond,parent.functor,functor);
}
}else_;
};
template
class if_functor_factory;
template
if_functor_factory if_(CondT cond){
return if_functor_factory(cond);
}
template
class if_functor_factory{
CondT cond;
public:
template
if_functor operator[](FunctorT functor){
return if_functor(cond,functor);
}
friend if_functor_factory if_(CondT cond);
private:
if_functor_factory(CondT &cond_):cond(cond_){}
};
struct is_longer_than_7{
bool operator()(const std::string& value){
return value.length() > 7;
}
};
struct when_longer_than_7{
void operator()(const std::string& value){
std::coutarray;
array.push_back("a");
array.push_back("abc");
array.push_back("abcde");
array.push_back("abcdefg");
array.push_back("abcdefgh");
array.push_back("abcdefghij");
std::for_each(array.begin(),array.end(),
if_(is_longer_than_7())[
when_longer_than_7()
].else_[
when_shorter_than_8()
]
);
}
文字列"a"は7文字以下ですね
文字列"abc"は7文字以下ですね
文字列"abcde"は7文字以下ですね
文字列"abcdefg"は7文字以下ですね
文字列"abcdefgh"は7文字より長い
文字列"abcdefghij"は7文字より長い
こんなの使えないけど… boost.lambda使うのがベストだと思います。