用於函式的與或非 函式
阿新 • • 發佈:2019-02-14
// and_or.cpp : 定義控制檯應用程式的入口點。 // #include "stdafx.h" #include <iostream> using namespace std; // restrain FuncL and FuncR must be bool(*)() template<typename FuncL , typename FuncR> class And { FuncL m_func_l; FuncR m_func_r; public: And(FuncL f1 , FuncR f2):m_func_l(f1),m_func_r(f2){} bool operator()() { return m_func_l() && m_func_r(); } }; template<typename FuncL , typename FuncR> And<FuncL , FuncR> and(FuncL f1 , FuncR f2) { return And<FuncL,FuncR>(f1,f2); } template<typename FuncL , typename FuncR> class Or { FuncL m_func_l; FuncR m_func_r; public: Or(FuncL f1 , FuncR f2):m_func_l(f1),m_func_r(f2){} bool operator()() { return m_func_l() || m_func_r(); } }; template<typename FuncL , typename FuncR> Or<FuncL , FuncR> or(FuncL f1 , FuncR f2) { return Or<FuncL,FuncR>(f1,f2); } template<typename Func> class Not { Func m_func; public: Not(Func f):m_func(f){} bool operator()() { return !m_func(); } }; template<typename Func> Not<Func> not(Func f) { return Not<Func>(f); } bool f1(){return true;} bool f2(){return false;} bool f3(){return true;} int _tmain(int argc, _TCHAR* argv[]) { cout<<not(and(or(f2,f3),f1))()<<endl; return 0; }