当前位置: 首页 > news >正文

做网站vpn多大内存外贸行业网站推广

做网站vpn多大内存,外贸行业网站推广,粮油移动端网页设计素材,旅游网站建设策划书范文如果一个模版看起来很头痛#xff0c;那么大概率这种模版是用来炫技#xff0c;没啥用的#xff0c;但是CRTP这个模版#xff0c;虽然看起来头大#xff0c;但是却经常被端上桌~ 奇异递归模板模式#xff08;Curiously Recurring Template Pattern, CRTP#xff09;是一…如果一个模版看起来很头痛那么大概率这种模版是用来炫技没啥用的但是CRTP这个模版虽然看起来头大但是却经常被端上桌~ 奇异递归模板模式Curiously Recurring Template Pattern, CRTP是一种 C 模板编程技巧用于实现静态多态。这种模式利用模板递归来允许一个类继承自一个模板类其中模板参数是该类自身。这种模式可以用于各种用途包括实现类型安全的单例模式、静态接口和性能优化。 CRTP基本概念 在 CRTP 模式中一个类 Derived 继承自一个模板类 Base这个模板类 Base 的模板参数是 Derived 自身。这样做的好处是允许在编译时确定 Derived 类型的信息从而在不需要运行时多态的情况下实现多态行为。以下是 CRTP 的基本示例展示了如何使用 CRTP 模式实现一个简单的静态接口 #include iostream using namespace std;template typename Child struct Base { void interface() {static_castChild*(this)-implementation(); } };struct Derived : BaseDerived { void implementation() {cout Derived implementation\n; } };int main() {Derived d;d.interface(); // Prints Derived implementationreturn 0; } CRPT模板展开 遇到CRPT不要慌一慌忘光光为了认识这种代码的本质我们不妨用Insights将其展开 C Insights #include iostream using namespace std;templatetypename Child struct Base {inline void interface(){static_castChild *(this)-implementation();}};/* First instantiated from: insights.cpp:13 */ #ifdef INSIGHTS_USE_TEMPLATE template struct BaseDerived {inline void interface(){static_castDerived *(this)-implementation();}// inline constexpr Base() noexcept default; };#endifstruct Derived : public BaseDerived {inline void implementation(){std::operator(std::cerr, Derived implementation\n);}// inline constexpr Derived() noexcept default; };int main() {Derived d;static_castBaseDerived(d).interface();return 0; } 可以发现展开的代码平平无奇一目了然。 Base 类是一个模板类它接受一个 Derived 类型作为模板参数。Derived 类继承自 BaseDerived从而在 Base 中能够静态地调用 Derived 类中的方法。 另一个CRPT实例 以下是一个稍微复杂一点使用 CRTP 实现策略模式的例子 #include iostream// CRTP 基类 template typename Derived class Strategy { public: void execute() {// 调用 Derived 类中的实现static_castDerived*(this)-perform(); } };// 具体策略 1 class ConcreteStrategy1 : public StrategyConcreteStrategy1 { public: void perform() {std::cout ConcreteStrategy1 implementation std::endl; } };// 具体策略 2 class ConcreteStrategy2 : public StrategyConcreteStrategy2 { public: void perform() {std::cout ConcreteStrategy2 implementation std::endl; } };int main() {ConcreteStrategy1 s1;ConcreteStrategy2 s2;s1.execute(); // 输出 ConcreteStrategy1 implementations2.execute(); // 输出 ConcreteStrategy2 implementationreturn 0; } 在这个示例中Strategy 是 CRTP 基类它定义了一个 execute 方法该方法调用派生类的 perform 方法。具体策略类 ConcreteStrategy1 和 ConcreteStrategy2 实现了不同的 perform 方法。这样Strategy 基类就能够静态地调用不同的策略实现。 总结 看到CRPT第一眼应该想到这玩意没什么特别的就是为了实现静态多态的。 静态体现在哪儿都是模版函数在编译期都已经展开函数调用是写死的多态体现在哪儿调佣基类的公用接口最后会执行子类的具体实现这一点和虚函数多态有点相似但这种多态是静态的。 所以CRPT就这点东西。
http://www.hkea.cn/news/14485095/

相关文章:

  • 做外贸没有网站需要注意什么问题手机怎么浏览国外网站
  • 可商用的图片素材网站网站移动适配
  • 阐述电子商务网站的建设要求做响应网站的素材网站有哪些
  • 女性手表网站网上做环评立项的网站是哪个
  • 空间服务 网站被黑万维网如何建设网站
  • 网站设计所用到的技术app推广拉新一手渠道代理
  • 长沙机械网站建设南昌广告公司
  • 广州网站app制作公司微信公众号官网登录入口手机版
  • 神州顺利办深一做网站crm系统排名
  • php 移动网站开发举例一个成功的网络营销案例
  • 描述建设一个网站的基本步骤新闻聚合网站开发
  • 石家庄网站建设咨询薇发布视频的平台大全
  • 泉州网站建设 推广西宁哪里做网站
  • 淘宝客导购网站建设?中小学门户网站建设
  • 建微信网站苏州网站建设制作开发公司
  • 做食品行业网站wordpress4.8.0
  • 搞定设计网站做的图怎么下载不了网站开发树形图
  • 家居网站开发项目计划书互联网公司市值
  • 域名网络的解析网站网站开发注册流程以及收费
  • 上海自聊自做网站全国企业信息系统网官网
  • 局域网内的网站建设wordpress 数据库sql
  • 我也来做外国网站购物wordpress d8 4.1
  • html5黑色网站网页网站建设软件有哪些
  • 不注册公司可以做网站吗郑州信息港
  • 公司网站做百度推广需要交费吗网易工作做网站工资奖金高吗
  • 太平洋建设集团招标网站问答社交网站开发
  • 光环时讯网站拍艺术照
  • 做视频点播网站要多少带宽郑州 服装 网站建设
  • 雍熙网站建设网页qq登录入口官网
  • 网站建设的专业知识网站建设项目分析