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

建设通网站的信息是哪里来的营销优化型网站怎么做

建设通网站的信息是哪里来的,营销优化型网站怎么做,手机网站建设定制,做租凭网站是经营性吗如果一个模版看起来很头痛,那么大概率这种模版是用来炫技,没啥用的,但是CRTP这个模版,虽然看起来头大,但是却经常被端上桌~ 奇异递归模板模式(Curiously Recurring Template Pattern, CRTP)是一…

如果一个模版看起来很头痛,那么大概率这种模版是用来炫技,没啥用的,但是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_cast<Child*>(this)->implementation();
}
};struct Derived : Base<Derived>
{
void implementation()
{cout << "Derived implementation\n";
}
};int main()
{Derived d;d.interface();  // Prints "Derived implementation"return 0;
}

CRPT模板展开

遇到CRPT不要慌,一慌忘光光,为了认识这种代码的本质,我们不妨用Insights将其展开 , C++ Insights

#include <iostream>
using namespace std;template<typename Child>
struct Base
{inline void interface(){static_cast<Child *>(this)->implementation();}};/* First instantiated from: insights.cpp:13 */
#ifdef INSIGHTS_USE_TEMPLATE
template<>
struct Base<Derived>
{inline void interface(){static_cast<Derived *>(this)->implementation();}// inline constexpr Base() noexcept = default;
};#endifstruct Derived : public Base<Derived>
{inline void implementation(){std::operator<<(std::cerr, "Derived implementation\n");}// inline constexpr Derived() noexcept = default;
};int main()
{Derived d;static_cast<Base<Derived>&>(d).interface();return 0;
}

可以发现,展开的代码平平无奇,一目了然。

  • Base 类是一个模板类,它接受一个 Derived 类型作为模板参数。
  • Derived 类继承自 Base<Derived>,从而在 Base 中能够静态地调用 Derived 类中的方法。

另一个CRPT实例

以下是一个稍微复杂一点使用 CRTP 实现策略模式的例子:


#include <iostream>// CRTP 基类
template <typename Derived>
class Strategy {
public:
void execute() {// 调用 Derived 类中的实现static_cast<Derived*>(this)->perform();
}
};// 具体策略 1
class ConcreteStrategy1 : public Strategy<ConcreteStrategy1> {
public:
void perform() {std::cout << "ConcreteStrategy1 implementation" << std::endl;
}
};// 具体策略 2
class ConcreteStrategy2 : public Strategy<ConcreteStrategy2> {
public:
void perform() {std::cout << "ConcreteStrategy2 implementation" << std::endl;
}
};int main() {ConcreteStrategy1 s1;ConcreteStrategy2 s2;s1.execute(); // 输出 "ConcreteStrategy1 implementation"s2.execute(); // 输出 "ConcreteStrategy2 implementation"return 0;
}

在这个示例中,Strategy 是 CRTP 基类,它定义了一个 execute 方法,该方法调用派生类的 perform 方法。具体策略类 ConcreteStrategy1ConcreteStrategy2 实现了不同的 perform 方法。这样,Strategy 基类就能够静态地调用不同的策略实现。

总结

看到CRPT,第一眼应该想到,这玩意没什么特别的,就是为了实现静态多态的。

  • 静态体现在哪儿,都是模版函数,在编译期都已经展开,函数调用是写死的
  • 多态体现在哪儿,调佣基类的公用接口,最后会执行子类的具体实现,这一点和虚函数多态有点相似,但这种多态是静态的。

所以,CRPT就这点东西。

http://www.yayakq.cn/news/50969/

相关文章:

  • 尖扎县公司网站建设wordpress变成英文
  • 门户网站开发架构网站建设需要做什么准备工作
  • 贵阳网站建设方案报价建设网站的好处有哪些
  • 比较还做的调查网站游戏优化大师官网
  • 未备案的网站 访问 hots房地产市场名词解释
  • 在凡科做的网站怎么推广营销型网站建设应该注意什么
  • 卖保健品可以自己做网站卖吗深圳高端网站建设电话
  • 南宁网站优化推广方案网站的建设目标是什么意思
  • 企业网站设计 优帮云国外搜索引擎
  • 天津餐饮团购网站建设成立一个公司需要多少钱
  • 网站开发保密协议范本企业网站如何制作
  • 电子商务网站例网站被加黑链
  • 接网站开发哪里好郑州seo推广
  • 重庆潼南网站建设哪家好北京软件开发公司图片
  • 网站建设游戏公司运城手机网站建设
  • 宝安网站建设zrarehoperun企业邮箱
  • 网站制作素材代码龙口网页定制
  • 甘肃做网站的公司有哪些建设一个网站论坛要多少钱
  • html5 手机网站 教程wordpress个性化
  • 广州网站开发创意设计公司做分销网站好吗
  • 网站死链接怎么处理企业做淘宝网站需要多少钱
  • 珊瑚绒毯移动网站建设西城网站制作公司
  • 如何开网站建设公司热狗seo顾问
  • 长春建站公司自家宽带怎么建设网站
  • 网站流量如何转化为钱如何建立网站做微商
  • 湛江企业建站系统wordpress网站结构
  • 公司建设网站的申请报告陕西免费做网站公司
  • 怎么注册企业视频号安徽seo报价
  • 嘉兴品牌网站建设景区网站建设方案 费用
  • 惠州网站制作专业app和手机网站