T.65: Use tag dispatch to provide alternative implementations of a function

T.65:使用标签分发提供函数的不同实现

Reason(原因)

  • A template defines a general interface.模板定义普遍接口。
  • Tag dispatch allows us to select implementations based on specific properties of an argument type.标签分发允许我们根据参数类型的特定属性选择实现方式。
  • Performance.性能

Example(示例)

This is a simplified version of std::copy (ignoring the possibility of non-contiguous sequences)

这是std::copy的简化版本(忽略非连续序列)

struct pod_tag {};struct non_pod_tag {};template struct copy_trait { using tag = non_pod_tag; };   // T is not "plain old data"template<> struct copy_trait { using tag = pod_tag; };         // int is "plain old data"templateOut copy_helper(Iter first, Iter last, Iter out, pod_tag){    // use memmove}templateOut copy_helper(Iter first, Iter last, Iter out, non_pod_tag){    // use loop calling copy constructors}templateOut copy(Iter first, Iter last, Iter out){    return copy_helper(first, last, out, typename copy_trait::tag{})}void use(vector& vi, vector& vi2, vector& vs, vector& vs2){    copy(vi.begin(), vi.end(), vi2.begin()); // uses memmove    copy(vs.begin(), vs.end(), vs2.begin()); // uses a loop calling copy constructors}

This is a general and powerful technique for compile-time algorithm selection.

这是一个可以在编译时选择算法的普遍和强大的技术。

Note(注意)

When concepts become widely available such alternatives can be distinguished directly:

当概念可以被普遍使用时,这样的选项可以直接区分:

template    requires Pod>Out copy_helper(In, first, In last, Out out){    // use memmove}templateOut copy_helper(In, first, In last, Out out){    // use loop calling copy constructors}

Enforcement(实施建议)

???

原文链接

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#t65-use-tag-dispatch-to-provide-alternative-implementations-of-a-function

新书介绍

《实战Python设计模式》是作者最近出版的新书,拜托多多关注!

本书利用Python 的标准GUI 工具包tkinter,通过可执行的示例对23 个设计模式逐个进行说明。这样一方面可以使读者了解真实的软件开发工作中每个设计模式的运用场景和想要解决的问题;另一方面通过对这些问题的解决过程进行说明,让读者明白在编写代码时如何判断使用设计模式的利弊,并合理运用设计模式。

对设计模式感兴趣而且希望随学随用的读者通过本书可以快速跨越从理解到运用的门槛;希望学习Python GUI 编程的读者可以将本书中的示例作为设计和开发的参考;使用Python 语言进行图像分析、数据处理工作的读者可以直接以本书中的示例为基础,迅速构建自己的系统架构。


觉得本文有帮助?请分享给更多人。

关注微信公众号【面向对象思考】轻松学习每一天!

面向对象开发,面向对象思考!

template标签_C++核心准则T.65:使用标签分发提供函数的不同实现相关推荐

  1. modbus软件开发实战指南_C++核心准则?GSL:指南支持库

    GSL: Guidelines support library GSL:指南支持库 The GSL is a small library of facilities designed to suppo ...

  2. 判断exception类型_C++核心准则T.44:使用函数模板推断类模板参数类型(如果可能)...

    T.44: Use function templates to deduce class template argument types (where feasible) T.44:使用函数模板推断类 ...

  3. blob转file对象_C++核心准则C.41:构造函数生成的对象应该被完全初始化

    C.41: A constructor should create a fully initialized object C.41构造函数生成的对象应该被完全初始化 Reason(原因) A cons ...

  4. java 成员变量声明顺序_C++核心准则讨论:按照成员声明的顺序定义和初始化成员变量...

    Discussion: Define and initialize member variables in the order of member declaration 讨论:按照成员声明的顺序定义 ...

  5. 无符号数的减法_C++核心准则ES.107:不要使用无符号数下标,使用gsl::index更好

    ES.107: Don't use unsigned for subscripts, prefer gsl::index ES.107:不要使用无符号数下标,使用gsl::index更好 Reason ...

  6. c++ file* 句柄泄漏_C++核心准则?讨论:持有没有被句柄管理的资源时切勿抛出异常...

    Discussion: Never throw while holding a resource not owned by a handle 讨论:持有没有被句柄管理的资源时切勿抛出异常 Reason ...

  7. c 函数多次声明_C++核心准则C.132:不要没有理由就将函数声明为虚函数

    岫玉 C.132: Don't make a function virtual without reason C.132:不要没有理由就将函数声明为虚函数 Reason(原因) Redundant v ...

  8. c++ h cpp文件如何关联_C++核心准则SF.5: .cpp文件必须包含定义它接口的.h文件

    SF.5: A .cpp file must include the .h file(s) that defines its interface SF.5: .cpp文件必须包含定义它接口的.h文件 ...

  9. unique函数_C++核心准则C.35:基类的析构函数必须满足的条件

    C.35: A base class destructor should be either public and virtual, or protected and nonvirtual 基类的析构 ...

最新文章

  1. SDUT_2121数据结构实验之链表六:有序链表的建立 (对建立的单项链表结构进行排序)
  2. boost::hana::experimental::print用法的测试程序
  3. 通过js引用外部脚本(方便直接在浏览器上调试抓取代码)
  4. java内部类的使用
  5. Linux chmod命令
  6. Linux下Redis的安装、配置操作说明
  7. Java并发(理论知识)—— 线程安全性
  8. 照片快照清除 android,如何在Android中删除和恢复照片
  9. python_知识点_字符串+数字+列表
  10. git submodule时fatal: Not a git repository (or any parent up to mount point /home)
  11. input子系统驱动学习之中的一个
  12. 广州“开四停四”交通限行,技术上是如何实现的?
  13. MATLAB更改初始工作路径
  14. Android BitmapShader实现圆形和圆角图片
  15. 宏碁4743G+固态硬盘(SSD)+机械硬盘(HHD)混合用
  16. appiume连接逍遥模拟器
  17. java鸡兔同笼用循环_Java使用for循环解决经典的鸡兔同笼问题示例
  18. 解析Hander消息处理机制
  19. LOFTER72D94FF8E0603DBE38066DB525285F1C
  20. 字体引入,不生效问题解决

热门文章

  1. React-Native 获取设备当前网络状态 NetInfo
  2. C++ cout格式化输出
  3. diag开关什么意思_双控开关接线图_一灯双控开关接线图_单联双控开关接线图_双控开关接线图实物图...
  4. python如何下载库_python中如何下载库
  5. cv2.imread()返回none时应如何解决
  6. python实例 87,88
  7. 【OpenCV 例程200篇】68. 连续周期信号的傅立叶级数
  8. 数据清洗-python实践
  9. 『TensorFlow』模型保存和载入方法汇总
  10. opencv之namedWindow,imshow出现两个窗口