Giter Club home page Giter Club logo

cpp-2x's Introduction

Examples of language features added in C++20

Examples of many C++ language features added in C++20. Click on any header for more details.

Aggregate initialization is no longer allowed for structs with explicitly defaulted or deleted constructors. examples

Allows conversions from an array of known bounds to a reference to an array of unknown bounds. When doing overload resolution, arrays with matching bounds are selected over arrays of unknown or non-matching sizes. examples

The size of newed arrays is now deduced, in the same way that arrays on the stack have deduced sizes. examples

Addition of several new attributes, including [[likely]], [[unlikely]] and [[no_unique_address]]. examples

A new syntax for initializing bitfields without using constructors. examples

Addition of a new type named char8_t. examples

Concepts are a new language feature that constrain template types and the properties that template types may (and many not) have. examples

Constructors can now be conditionally explicit. examples

Consteval functions can be called where all of their arguments are constant expressions, but not if any of the arguments are non-constant. They are called 'immediate functions': every call to a consteval function must produce a compile-time constant expression. Variables cannot be declared consteval. examples

Not all class members need to be initialized by constexpr constructors, but reading (using) uninitialized members in constexpr contexts in an error. examples

Initialize variables at compiletime. examples

Functions that can suspend exection (storing their state in a object on the heap) and be resumed later. examples

Allows structured bindings for custom classes and structs, not just for pairs and tuples. examples

Comma operators in subscript expressions are deprecated. examples

Deprecates some uses of volatile, and some uses of volatile variables. examples

A new syntax for named initializers for struct, class and union members. examples

Many macros have been added which test for C++ features. examples

The keyword 'typename' is no longer required in many cases, including in the default value of a template parameter, in the return type of a function declaration or definition, in class scoped function definitions and class-scoped typedefs, among others. examples

Lambda functions can now be used in default-initialized class members. examples

Moves, rather than copies, are used more often. examples

Prefer the copy constructor when initializing a variable with an initializer list if the list consists of a single element compatible with the deduced type. examples

In some use cases (but not all), ::std::is_constant_evaluated evaluates to true if the enclosing function is constant-evaluated. examples

Addition of generic lambda functions (using auto parameters), lambda functions with template parameters, concept-constrained template parameters and variadic templates. examples

Declaring a typedef of a stateless lambda is allowed, lambdas can now be default-constructed, and a stateless lambda can be default-constructed and copy assigned. examples

::std::make_shared supports array types. examples

Adds functions for: *. The midpoint of two integers/floats/doubles (::std::midpoint in );

  • A point along the linear interpolation of two values (::std::lerp in );
  • to_array, to convert raw arrays to ::std::array objects;
  • make_shared an array;
  • ::std::string has new member functions to test whether it begins or ends with another expression.

examples

Modules are an alternative to #include. #included files are pasted into the main translation unit as text and then parsed. Modules, on the other hand, are #imported as binary entities.

The inline keyword is now allowed to appear in nested namespace definitions. Functions in inline namespaces are referenced as if they were declared in the nearest upward non-inlined namespace. examples

Some additional non-type template parameters are now allowed. examples

An alternative overload for class member delete. If the overload exists then it is responsible for calling the class destructor. examples

Conversions from pointer (or nullptr) to bool are now narrowing. examples

Prior to C++20, const&-qualified member functions could not be used as function pointers on rvalue objects. examples

Private subtypes (subclasses) can be used to partly or fully specialize class templates. examples

Free begin()/end() functions are now preferred to member begin/end functions if at least one of the latter does not exist. examples

An additional variable is allowed in range-based for loops. examples

Ranges is "STL v2". It is a drop-in replacement for many of the components of the STL library.

There are major differences between ranges and pre-ranges code. These include (but are not limited to):

  • Ranges use C++20 concepts to enforce conditions. This greatly improves the clarity of error messages in template code.
  • A range is itself a concept: it is a type that supports ::std::ranges::begin(object) and ::std::ranges::end(object). The type of the end iterator need not be the same as the iterator type returned by begin() - they need only be comparable. This allows, among other things, for infinite ranges: use ::std::unreachable_sentinel for an iterator that never compares positively with another range iterator.
  • Many algorithms, eg. ::std::ranges::sort, accept a reference to a range container, not just a pair of begin/end iterators.

examples

Addition of atomic shared pointers. examples

Adds the function ::std::source_location::current() which contains information about the source line, column and enclosing function at the call site. examples

The "spaceship operator", <=>, is a class-based comparison operator that, for objects A and B, determines whether A < B, A == B or A > B. The spaceship operator works in much the same way as strcmp: < 0 means "A < B", == 0 means "A == B" and > 0 means "A > B". examples

Spans are objects that reference contiguous sequences of objects. Spans have constant copy/move time complexity and do not own the data that they point to. You can modify the underlying data via a span. examples

Structured bindings can be captured by lambdas by value and by reference. examples

Template parameter deduction works even if the type is an alias of another template type. examples

Addition of using enum. Enum values do not need to be prefixed with the enum class name in the same block as this instruction. examples

License

All of the code in this repository (including in documentation and in README.md files) is licensed under the GNU General Public License, version 3. See https://www.gnu.org/licenses/.

cpp-2x's People

Contributors

j040222 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.