c++ - How to chain/queue up functors? -


i not using c++11 (otherwise use lambdas)

i have iterators input data structure , output data structure. want operations on input data , store result in output. note start , end types may different (int -> float example).

i tried model function after standard algorithms , gave following prototype

template<class input_itr, output_itr> void f(input_itr in_it, input_itr in_it_end, output_itr out_it, contextstuff) 

the function many different things depending on context. function needs use 1 functor, line of code looks this

transform(in_it, in_it_end, out_it, functor1()); 

but function wants use series of functors on each data element. there way can create chain of functors single functor use in transform? functors chain known @ compile type.

for example

transform(in_it, in_it_end, out_it, chain(functor1(), functor2())); 

performs functor1 on *in, functor2 on result, stores in *out.

i can inherit functors unary_function solution.

try this:

template<class callable1, class callable2> struct chain : public std::unary_function<     typename callable2::argument_type, typename callable1::result_type> {     chain(const callable1 &f1, const callable2 &f2) : f1(f1), f2(f2) {}      typename callable1::result_type operator () (            typename callable2::argument_type param)      {          return f1(f2(param));     } private:     callable1 f1;     callable2 f2; };  template<class callable1, class callable2> chain<callable1, callable2> chain(const callable1 &f1, const callable2 &f2) {     return chain<callable1, callable2>(f1, f2); } 

the chain class functor combines 2 other functors. since it's unary_function should able combine further. chain function lets create chain instance without having worry types.

usage example: http://ideone.com/7qpmeu


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -