std::unary_functionã®åå¨æ義ãããããããªããªã£ãã
ååã®ã¨ã³ããªã§
ãã ããã®ä¾ã®std::unary_functionã¨ãstd::binary_functionã¨ãã¯æ¬å½ã«virtualãã¹ãã©ã¯ã¿ããªãè¨è¨ã§ããã®ï¼ã¨ããç¹ã¯ã¡ãã£ã¨çåã§ããã
ä¾ãã°std::unary_functionã®æ´¾çã¯ã©ã¹ã¨ãã¦ããç°å¢ã¸ã®åç §ãæã¤é¢æ°(ã¤ã¡ã¼ã¸ã¨ãã¦ã¯ã¯ãã¼ã¸ã£)ããä½ã£ã¦ããã¤ã¯ãã¹ãã©ã¯ã¿ã§ä½ããããªãã¨ãã¡ã ã¨ãã¦ãunary_functionãªãã¸ã§ã¯ãã¨ãã¦ã³ã³ããã«ã¤ã£ããã§ä½ãããå¾ã«ããªãã¸ã§ã¯ããunary_functionã¨ãã¦ç ´å£ããã¨ä¸å®ãªåä½ã«ãªãã¨ããã®ã¯ã¡ãã£ã¨ä¸å®ã«æãã¾ãã
ã¨æã£ã¦ãã¨ããããstd::unary_functionã¨ããããã£ã¦ã¿ãã°çç±ããããããªã¨æã£ã¦ããã£ã¦ã¿ãã
ç·´ç¿åé¡ã¨ãã¦ãä¸å¼æ°é¢æ°ãåãåã£ã¦ãã®å°é¢æ°ãè¿ããããªé«éé¢æ°ãæ¸ãã¦ã¿ãã
ã¨ããããä¸å¼æ°é¢æ°ã®è¡¨ç¾ã¯std::unary_functionã§ããããã¨ãæã£ã¦æ¸ãå§ãã¦ã
template<T>
unary_function<T, T> deriv(unary_function<T, T> f, T dx);
ã¿ããã«ãªã·ã°ããã£ã§ããããã¨æã£ãã®ã ãã©ãããããè¦ã㨠unary_function èªä½ã«ã¯ operator() ã宣è¨ããã¦ããªãã®ã§ããã§ã¯é§ç®ãunary_functionã¯ããããç®çã«ä½¿ããã®ã§ã¯ãªããããã
functionalããããè²ã çºãã¦ããããpointer_to_unary_functionãªãã¯ã©ã¹ããããããã¤ã¯unary_functionã®æ´¾çã¯ã©ã¹ã§ãããoperator() ãå®è£ ãããã¡ã³ã¯ã¿ã¯ã©ã¹ã½ãã£ãããããå°é¢æ°deriveredã¯ã©ã¹ã¯pointer_to_unary_functionã®æ´¾çã¯ã©ã¹ã§ãããpointer_to_unary_functionãªãã¸ã§ã¯ããã¡ã³ãã¨ãã¦æã¤æãã«ãã¦ã
template<class T> class derivered : public pointer_to_unary_function<T, T> { public: derivered(/*...*/) : /*...*/ {} T operator()(T x) const { return ( f(x + dx) - f(x) ) / dx ); } private: pointer_to_unary_function<T, T> f; T dx; };
ã¿ããã«è¡ãããã¨æã£ãããpointer_to_unary_functionã®operator()ã¯virtualãããªãã®ã§ç¶æ¿ã¯ç¡çã£ã½ãããã¼ããå°é¢æ°ã¯ä¸å¼æ°é¢æ°ã®ãµãã¯ã©ã¹ã«ãããæ°ããããã©ãªããã¾ãããã«virtualãã¹ãã©ã¯ã¿ããªãããå±éºãå±ãªãã
ãããæ½è±¡ã¯ã©ã¹functorã¿ãããªã®ãä½ã£ã¦ãderiveredã¯ã©ã¹ã¯functorã®æ´¾çã¯ã©ã¹ã§ãããfunctorãªãã¸ã§ã¯ããåãåã£ã¦deriveredãªãã¸ã§ã¯ããè¿ããããªderivé¢æ°ã§ãä½ããã¨ãããã¨ã«ãããã¨ããããCï¼ï¼ã決ã¾ãã®ãfunctorããã¤ã³ã¿ã§åãåããconståç §ã§åãåããã¨ããã¨ããã§ããã©ããããªã£ããã¾ãå¤åãã¤ã³ã¿ã§åãåãã®ãæ£ããã®ã ããããã¨deriveredã¯é¢æ°ãã¤ã³ã¿ãã¡ã³ãã«æã¤ã¹ãããfunctorãã¡ã³ãã«æã¤ã¹ããã¨ããæ©ã¾ãããtemplateã§æ¸ãã¡ãã£ã¦ããããã©ã¨ããªãã¨ãæ©ã¿ãªããããã£ã±ãé«éé¢æ°æ¸ãã¨ãã¯GCã欲ããã¨ããçµè«ã«éããã
#include<functional> #include<iostream> #include<cmath> using std::unary_function; using std::pointer_to_unary_function; template<class T, class Func> class derivered : public unary_function<T, T> { public: derivered(const Func& f, T dx) : f(f), dx(dx) {} T operator()(T x) const { return ( f(x + dx) - f(x) ) / dx; } private: Func f; T dx; }; template<class T, class Func> derivered<T, Func> deriv(Func f, T dx){ return derivered<T, Func>(f, dx); } using std::ptr_fun; using std::cout; using std::endl; int main() { const double PI(3.141592); pointer_to_unary_function<double, double> f = ptr_fun(sin); cout << f(PI) << endl; cout << deriv(f, 0.0001)(PI) << endl; cout << deriv(cos, 0.0001)(PI) << endl; return 0; }
ããã§æ©ã¿ãªããæ¸ããã®ããããªãã ãã©ãé§ç®é§ç®ã£ã½ãæãã
æ¸ãã¦ã¦æã£ãã®ã¯ãããããfunctorãããªã¢ã«ãã£ãã¯ã«ãããã¨ããã¨ãçµå±ãã¤ã³ã¿çµç±ã§ä½¿ããããªã£ã¦ãããããã¨é¢æ°ã½ãå¼ã³åºãã¨ãã«ããªãã¡ã¬ã³ã¹ãã¦ãããªãããããªããªããã§ãããã¯ç¾å¨ã®é¢æ°ãã¤ã³ã¿ã®æåã¨ä¸æ´åãªã®ã§ï¼ããªãã¡ã¬ã³ã¹ããªãããããªãé¢æ°ãã¤ã³ã¿ãªãã¦ANSI以åã®Cã¿ããã ï¼ï¼åºæ¬ãããã使ãæ¹ãããªãããã«ãunary_functionãpointer_to_unary_functionãä»®æ³é¢æ°ãªãã®è¨è¨ãªã®ããªã¨æã£ããã§ãããããã¨unary_functionã®åå¨æ義ãå
¨ãåãããªãã