⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ex14_1.cpp

📁 一些同学问我
💻 CPP
字号:
/* Code for exercise 14.1. |  | "C++ Solutions--Companion to The C++ Programming Language, Third Edition" | by David Vandevoorde; Addison-Wesley 1998; ISBN 0-201-30965-3. | | Permission to use, copy, modify, distribute and sell this software | and its documentation for any purpose is hereby granted without fee. | The author makes no representations about the suitability of this | software for any purpose.  It is provided "as is" without express or | implied warranty. `----------------------------------------------------------------------*/#include <iostream.h>template<class R = void, class T1 = void, class T2 = void>struct STC {   typedef R (*PF)(T1, T2);   STC(PF pf): prev_pf_(cur_pf_) { cur_pf_ = pf; }   ~STC() { cur_pf_ = prev_pf_; }   R operator()(T1 a, T2 b) { return cur_pf_(a, b); }private:   PF prev_pf_;   static PF cur_pf_;};template<class R, class T1, class T2>typename STC<R, T1, T2>::PF STC<R, T1, T2>::cur_pf_ = 0;template<class R, class T1>struct STC<R, T1, void> { // Partial specialization   typedef R (*PF)(T1);   STC(PF pf): prev_pf_(cur_pf_) { cur_pf_ = pf; }   ~STC() { cur_pf_ = prev_pf_; }   R operator()(T1 a) { return cur_pf_(a); }private:   PF prev_pf_;   static PF cur_pf_;};template<class R, class T1>typename STC<R, T1, void>::PF STC<R, T1, void>::cur_pf_ = 0;template<typename R>struct STC<R, void, void> { // Partial specialization   typedef R (*PF)();   STC(PF pf): prev_pf_(cur_pf_) { cur_pf_ = pf; }   ~STC() { cur_pf_ = prev_pf_; }   R operator()() { return cur_pf_(); }private:   PF prev_pf_;   static PF cur_pf_;};template<class R>typename STC<R, void, void>::PF STC<R, void, void>::cur_pf_ = 0;int g(int i) {   return i*i;}int f() {   STC<int, int> gc(&g);   std::cout << gc(4) << '\n';   return 3;}int main() {   STC<int> fc(&f);   {      STC<int, int> gc(&g);      std::cout << gc(7) << '\n';   }   std::cout << fc() << '\n';   return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -