C++核心准则T.65:使用标签分发提供函数的不同实现
时间:2022-07-25
本文章向大家介绍C++核心准则T.65:使用标签分发提供函数的不同实现,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
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<class T> struct copy_trait { using tag = non_pod_tag; }; // T is not "plain old data"
template<> struct copy_trait<int> { using tag = pod_tag; }; // int is "plain old data"
template<class Iter>
Out copy_helper(Iter first, Iter last, Iter out, pod_tag)
{
// use memmove
}
template<class Iter>
Out copy_helper(Iter first, Iter last, Iter out, non_pod_tag)
{
// use loop calling copy constructors
}
template<class Iter>
Out copy(Iter first, Iter last, Iter out)
{
return copy_helper(first, last, out, typename copy_trait<Iter>::tag{})
}
void use(vector<int>& vi, vector<int>& vi2, vector<string>& vs, vector<string>& 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<class Iter>
requires Pod<Value_type<iter>>
Out copy_helper(In, first, In last, Out out)
{
// use memmove
}
template<class Iter>
Out 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
- 锂离子电池发明人:自动驾驶汽车电池需要更加耐用
- Linux中MySQL5.6编译安装与MySQL5.7二进制安装步骤
- Nginx服务编译安装、日志功能、状态模块及访问认证模式实操
- 快速入门系列--WebAPI--03框架你值得拥有
- 快速入门系列--MVC--06视图
- 腾讯入局物业管理 欲改造传统服务?
- ExtJs学习笔记(4)_EditorGridPanel(可编辑的网格控件)
- ansible批量管理软件部署及剧本
- 快速入门系列--MVC--02路由
- Javascript生成GUID
- 快速入门系列--MVC--04模型
- 快速入门系列--MVC--03控制器和IOC应用
- ExtJs学习笔记(3)_GridPanel[XML做数据源]
- 快速入门系列--MVC--05行为
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法