Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How does C++ define the concept for template parameters

Shulou Source: shulou.com Published: 2022-06-01 20:48:47 09月30日 Update

This article mainly explains "how C++ defines concepts for template parameters". The explanation content in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian to study and learn "how C++ defines concepts for template parameters" together.

T.10: Define concepts for all template parameters

Reason

Correctness and readability. The assumed meaning (syntax and semantics) of a template argument is fundamental to the interface of a template. A concept dramatically improves documentation and error handling for the template. Specifying concepts for template arguments is a powerful design tool.

correctness and readability. The assumed meaning (syntax and semantics) of a template parameter is the basis of the template interface. The concept greatly improves template documentation and error handling. Defining concepts for template parameters is a powerful design tool.

Example

template

// requires Input_iterator

// && Equality_comparable

Iter find(Iter b, Iter e, Val v)

{

// ...

}

or equivalently and more succinctly:

Or use the following functionally equivalent, but simpler, approach:

template

// requires Equality_comparable

Iter find(Iter b, Iter e, Val v)

{

// ...

}Note

"Concepts" are defined in an ISO Technical Specification: concepts. A draft of a set of standard-library concepts can be found in another ISO TS: ranges Concepts are supported in GCC 6.1 and later. Consequently, we comment out uses of concepts in examples; that is, we use them as formalized comments only. If you use GCC 6.1 or later, you can uncomment them:

"Concept" is defined by ISO technical specifications: concepts. A preliminary version of a standard library concept can be found in another ISO specification: ranges. GCC 6.1 supports concepts. So we commented out concepts in our example code; that is, we used them as standard comments. If you are using a later version of GCC 6.1, you can open comments.

template

requires Input_iterator

&& Equality_comparable

Iter find(Iter b, Iter e, Val v)

{

// ...

}Note

Plain typename (or auto) is the least constraining concept. It should be used only rarely when nothing more than "it's a type" can be assumed. This is typically only needed when (as part of template metaprogramming code) we manipulate pure expression trees, postponing type checking.

A straightforward type name (or auto) is the concept of minimal constraint. It should be used sparingly and limited to expressing "it is a type." This is usually only necessary (as part of template metaprogramming) when we operate on pure expression trees, deferred type checking.

Thank you for reading, the above is "C++ how to define the concept of template parameters" content, after the study of this article, I believe we have a deeper understanding of how C++ defines the concept of template parameters, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Tags: Templates concepts parameters clocking comments types learning content examples technology standards versions specifications minimum concise necessary powerful that is code Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Apple Microsoft MySQL Docker