Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the different implementations of C++ 's use of tag distribution to provide functions

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "what are the different implementation methods of C++ using tags to provide functions?". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations! I hope you can read it carefully and be able to achieve something!

T.65: use label distribution to provide different implementations of functions

Reason (reason)

A template defines a general interface.

The template defines the universal interface.

Tag dispatch allows us to select implementations based on specific properties of an argument type.

Label distribution allows us to choose the implementation based on the specific properties of the parameter type.

Performance.

Performance

Example (sample)

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

This is a simplified version of std::copy (ignoring discontiguous sequences)

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.

This is a universal and powerful technique that allows you to select algorithms at compile time.

Note (Note)

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

When concepts can be widely used, such options can directly distinguish between:

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

} this is the end of the content of "what are the different implementations of C++ 's use of tag distribution to provide functions". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report