热门关键词:

奇怪的重复模板模式(CRTP)

添加时间:2019-09-05 15:44:21

来源:

浏览:

通过奇怪的重复模板模式(CRTP)可以完全避免使用VPtr和VTable。CRTP是C ++中的一种设计模式,其中类X派生自使用X本身作为模板参数的类模板实例化。更一般地,它被称为F结合多态性。

filter_none

编辑

play_arrow


brightness_4

// Image program (similar to above) to demonstrate 

// working of CRTP 

#include <iostream> 

#include <chrono> 

using namespace std; 

  

typedef std::chrono::high_resolution_clock Clock; 

  

// To store dimensions of an image 

class Dimension 

public: 

    Dimension(int _X, int _Y) 

    { 

        mX = _X; 

        mY = _Y; 

    } 

private: 

    int mX, mY; 

}; 

  

// Base class for all image types. The template 

// parameter T is used to know type of derived 

// class pointed by pointer. 

template <class T> 

class Image 

public: 

    void Draw() 

    { 

        // Dispatch call to exact type 

        static_cast<T*> (this)->Draw(); 

    } 

    Dimension GetDimensionInPixels() 

    { 

        // Dispatch call to exact type 

        static_cast<T*> (this)->GetDimensionInPixels(); 

    } 

  

protected: 

    int dimensionX, dimensionY; 

}; 

  

  

// For Tiff Images 

class TiffImage : public Image<TiffImage> 

public: 

    void Draw() 

    { 

        // Uncomment this to check method dispatch 

        // cout << "TiffImage::Draw() called" << endl; 

    } 

    Dimension GetDimensionInPixels() 

    { 

        return Dimension(dimensionX, dimensionY); 

    } 

}; 

  

// There can be more derived classes like PngImage, 

// BitmapImage, etc 

  

// Driver code 

int main() 

    // An Image type pointer pointing to Tiffimage 

    Image<TiffImage>* pImage = new TiffImage; 

  

    // Store time before virtual function calls 

    auto then = Clock::now(); 

  

    // Call Draw 1000 times to make sure performance 

    // is visible 

    for (int i = 0; i < 1000; ++i) 

        pImage->Draw(); 

  

    // Store time after virtual function calls 

    auto now = Clock::now(); 

  

    cout << "Time taken: "

         << std::chrono::duration_cast 

         <std::chrono::nanoseconds>(now - then).count() 

         << " nanoseconds" << endl; 

  

    return 0; 

输出:


所用时间:732纳秒

见这对上述结果。


虚拟方法与CRTP基准测试

使用虚拟方法所用的时间为2613纳秒。CRTP的这种(小)性能增益是因为绕过了VTable调度的使用。请注意,性能取决于许多因素,如使用的编译器,虚拟方法执行的操作。不同运行中的性能数字可能不同,但CRTP预计会有(小)性能提升。

注意:如果我们在CRTP中打印类的大小,可以看出VPtr不再保留4个字节的内存。


 

cout << sizeof(图片)<< endl; 


需要开发软件,app,管理系统,就找我们成都软件公司吧。

用户名 Name
评论 Comment

软件开发  相关内容

——
14

2021-07

OracleNetsuite的建设标题

到目前为止,我们已经为我们OracleNetsuite的标题创建了导航栏。完成标题的下一件事是在图像上方包含图像和文本,如下面的屏幕截图所示:让我们再次查看index.html 文件中标题的部分代码。代码中突出显示的部分显示了标题的图像菜单:要完成图像菜单,我们首先需要在 id … [了解更多]

14

2021-07

OracleNetsuite响应式和移动…

响应式网站:响应式网站是旨在适合所有类型的设备并调整网站布局以最适合屏幕尺寸的网站。无需制作任何其他设备版本的网站以适应小型设备。移动网站:移动网站是专为适应手机、平板电脑等特别小的设备屏幕而设计的网站。需要制作网站的桌面版本以适应移动设备特定的桌面屏幕。响应式网站和移动网站的区… [了解更多]

08

2021-06

OracleNetsuite ​Doc…

OracleNetsuitePython 提供了许多分发 Python 项目的方法。其中一种方法是使用一种称为 Docker 的重要技术。Docker 是一个开源应用程序,允许管理员使用容器创建、管理、部署和复制应用程序。它基本上是一个平台,使开发人员能够通过将应用程序放入容器中… [了解更多]

22

2021-05

OracleNetsuiteERP在公开…

财务负责人戴了两顶帽子:一是遵守法规,以确保公司的行为和会计正确无误,并遵守公司开展业务的不同司法管辖区的法规;二是遵守法规。一种战略,确保公司达到财务里程碑和成功指标。当一家公司上市时,包括通过特殊目的收购公司(SPAC)上市时,这两个角色尤其重要。Oracle NetSuit… [了解更多]

08

2021-03

星空RPA智能链接方案

公司财务人员中记账人员的工作内容:1、从钉钉中下载审批完成的8种审批类型的单据数据,包含合同付款、费用报销等2、记账人员根据付款的性质及费用归属,把记账分成6种形式:合同付款(工程、成本)、合同付款(其他)、非合同付款(工程、成本)、非合同付款(其他)、费用报销(工程、成本)、费… [了解更多]

关注

深信服

  • 地 址:成都市人民南路四段成科西路三号 863国家孵化园
  • 电 话:18215 660330
  • 手机:18215 660330
  • 传 真:18215 660330
  • 邮 箱:179001057@qq.com
  • 邮政编码:610000