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 four new features of Craft 20?

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article shows you what are the four new features of Craft 20, which are concise and easy to understand. I hope you can gain something through the detailed introduction of this article.

Clipped 20 (version 2020 of the C++ programming language standard) will be a very significant update to the C++ language and will introduce a large number of new features to the language. Recently, C++ developer Rainer Grimm is introducing the new features of Clover 20 through a series of blog posts. This is the first article in this series that has been updated so far, focusing on Big Four (four new features: concept, scope, co-programming, and modules) and the core language (including some new operators and indicators) of Clover 20.

There are a lot of updates to Category 20, and the figure above shows an overview of updates to Category 20. The following author first introduces the compiler support of Clover 20, and then introduces The Big Four (four new features) and new features in the core language.

Compiler support for Clipper 20

The easiest way to adapt to new features is to try them out. So then we are faced with the question: which compilers support which features of Category 20? In general, http://cppreference.com/compiler_support_ can provide answers in core languages and libraries.

In a nutshell, the new GCC, Clang, and EDG compilers provide the best support for core languages. In addition, the MSVC and Apple Clang compilers support many of the clipped 20 features.

The situation with respect to libraries is similar. GCC has the best support for libraries, followed by Clang and MSVC compilers.

The screenshot above shows only the first part of the corresponding table, and you can see that the performance of these compilers is not very satisfactory. Even if you are using brand new compilers, these compilers still do not support many new features. In general, you can find ways to try these new features. Here are two examples:

Concept: GCC supports the previous version of the concept

There is a draft implementation on std::jthread:GitHub.

To put it simply, the problem is not that serious. With a few adjustments and modifications, many new features can be tried. If necessary, I will mention how to make such a change.

Four New Features of Clipper 20

Concept (concept)

The key idea of general programming with templates is to define functions and classes that can be used through various types (type). However, the problem of using the wrong type often occurs when instantiating the template, and the result is usually a few pages of hard-to-understand error messages.

Now that the concept has come, this problem can be put to rest. Concepts allow you to write requirements for templates, which the compiler can check. Concepts revolutionize the way we think and write common code. The reasons are as follows:

The requirements of the template are part of the interface

Function overloading or specialization in class templates can be based on concepts

Because the compiler can compare the requirements of the template parameters with the actual template parameters, it can get better error information.

But that's not all.

You can use predefined concepts, or you can define your own

The usage of auto and concept are unified. Instead of using auto, you can use concepts

If a function declaration uses a concept, it automatically becomes a function template. As a result, writing a function template becomes as simple as writing a function.

The following code snippet shows the definition and use of a simple concept Integral:

Template

Concept bool Integral () {

Return std::is_integral::value

}

Integral auto gcd (Integral auto a

Integral auto b) {

If (b = = 0) return a

Else return gcd (b, a% b)

}

The concept of Integral requires the type parameter T in std::is_integral::value. The function std::is_integral::value comes from the type-traits library, which can check the compile time for integers at T. If the value of std::is_integral::value is true, there is no problem. If it is not true, you will receive a compile time error.

Gcd algorithm is based on Euclidean algorithm to determine the maximum common divisor (greatest common divisor). I used this abbreviated function template syntax to define gcd. Gcd requires that its parameters and return types support the concept Integral. Gcd is a class of function templates that require both parameters and return values. When I delete the syntax syntactic sugar, you may be able to see the true nature of gcd. The following code is semantically equivalent to the gcd algorithm:

Template

Requires Integral ()

T gcd (T a, T b) {

If (b = = 0) return a

Else return gcd (b, a% b)

}

If you haven't seen the true nature of gcd, I'll post a concept article in a few weeks.

Range Library (Ranges Library)

The scope library is the first customer of the concept. The algorithms it supports meet the following conditions:

Can operate directly on the container; no iterator is required to specify a range

Can be loosely evaluated.

It can be combined.

To put it simply: the scope library supports the function mode (functional patterns).

The code may be clearer than the language description. The following function shows the composition of the function with a vertical bar symbol:

# include

# include

# include

Int main () {

Std::vector ints {0, 1, 2, 3, 4, 5}

Auto even = [] (int I) {return 0 = = I% 2;}

Auto square = [] (int I) {return I * I;}

For (int I: ints | std::view::filter (even) |

Std::view::transform (square)) {

Std::cout

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

Development

Wechat

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

12
Report