Effective C++ 44. Factor parameter-independent code out of templates
来源:互联网 发布:djay pro 破解版 mac 编辑:程序博客网 时间:2024/06/07 16:39
template<typename T, std::size_t n>class squareMatrix {public: void invert();};
squareMatrix<double, 5> sm1;sm1.invert();squareMatrix<double, 10> sm2;sm2.invert();
two copies of invert will be instantiated here. This is a classic way for template-induced code bloat to arise.
template<typename T>class SquareMatrixBase{protected: SquareMatrixBase(std::size_t n, T *pMem): size(n), pData(pMem) { } void setDataPtr(T* ptr) { pData = ptr; }private: std::size_t size; T *pData;};template<typename T, std::size_t n>class SquareMatrix: private SquareMatrixBase<T> {public: squareMatrix(): SquareMatrixBase<T>(n, data) {}private: T data[n*n];};
put the data for each matrix on the heap
template<typename T, std::size_t n>class SquareMatrix: private SquareMatrixBase<T> {public: SquareMatrix(): SquareMatrixBase<T>(n, 0), pData(new T[n*n]) { this->setDataPtr(pData.get()); }private: boost::scoped_array<T> pData;};
These functions use the “this->” notation, because function names in templatized base classes are hidden from derived classes.
但是在vs2017 中没有 “this->” 也是可以编译通过的。
阅读全文
0 0
- Effective C++ 44. Factor parameter-independent code out of templates
- [翻译] Effective C++, 3rd Edition, Item 44: 从 templates(模板)中分离出 parameter-independent(参数无关)的代码(上)
- [翻译] Effective C++, 3rd Edition, Item 44: 从 templates(模板)中分离出 parameter-independent(参数无关)的代码(下)
- Writing endian-independent code in C
- Mybatis 错误信息:Parameter index out of range
- Parameter index out of range 错误
- MYSQL: Parameter index out of range
- java.sql.SQLException: Parameter index out of range
- java.sql.SQLException: Parameter index out of range (0 < 1 )
- java.sql.SQLException: Parameter index out of range (0 < 1 ).
- Effective Objective-C 2.0: Item 32: Beware of Memory Management with Exception-Safe Code
- eclipse code templates
- Java Code Templates
- Netbeans Code Templates
- 【Eclipse】Code Templates 设置
- variadic templates sample code
- Eclipse Code Templates设置
- Eclipse Code Templates设置。
- mysql explain type
- expect安装以及进程查杀重启
- 容易被大多数人忽视的STM32串口DMA问题
- solr5以上版本配置到tomcat运行
- Centos6.9 安装zabbix-server
- Effective C++ 44. Factor parameter-independent code out of templates
- zookeeper+kafka+storm+flume 环境搭建
- java电子商务源码 b2b2c o2o
- 引用返回
- 阿里云公网访问Redis
- RPF检查在组播数据转发中的应用
- springboot系列文章阶段性总结
- 并发编程实现模型之(三)Producer-Consumer模式
- HTML笔记(二)