成都创新互联网站制作重庆分公司

C++使用标签分发提供函数的不同实现方法是什么

本篇内容介绍了“C++使用标签分发提供函数的不同实现方法是什么”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

水富网站建设公司创新互联,水富网站设计制作,有大型网站制作公司丰富经验。已为水富成百上千家提供企业网站建设服务。企业网站搭建\成都外贸网站制作要多少钱,请找那个售后服务好的水富做网站的公司定做!

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"

template
Out copy_helper(Iter first, Iter last, Iter out, pod_tag)
{
   // use memmove
}

template
Out copy_helper(Iter first, Iter last, Iter out, non_pod_tag)
{
   // use loop calling copy constructors
}

template
Out 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
}

template
Out copy_helper(In, first, In last, Out out)
{
   // use loop calling copy constructors
}

“C++使用标签分发提供函数的不同实现方法是什么”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!


网站标题:C++使用标签分发提供函数的不同实现方法是什么
当前地址:http://cxhlcq.com/article/pchjsd.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部