C++20 standard approved

ISO C++ Standards Committee approved international standard "C ++ 20". The possibilities presented in the specification, with the exception of isolated cases, are supported in compilers GCC, Clang ΠΈ Microsoft Visual C ++. Supporting C++20 standard libraries implemented as part of the project Boost.

In the next two months, the approved specification will be at the stage of preparing a document for publication, at which work will be carried out on editorial correction of spelling errors and typographical errors. In early November, the resulting document will be submitted to ISO for publication under the formal name ISO/IEC 14882:2020. In the meantime, the committee has already begun work on the next C++23 (C++2b) standard and will consider possible innovations.

All features C ++ 20 (code examples):

  • "Concepts", template extensions, have been added to define a set of template parameter requirements that, at compile time, limit the set of arguments that can be accepted as template parameters. The concepts can be used to avoid logical inconsistencies between the properties of the data types used within the template and the properties of the data types of the input parameters.

    template
    concept EqualityComparable = requires(T a, T b) {
    { a == b } -> std::boolean;
    { a != b } -> std::boolean;
    };

  • Adopted expansion to work with modules that can be used instead of header files. Modules provide a new way to organize source code based on the definition of component boundaries, without #include header files.
  • Macro __VA_OPT__ for adaptive expansion of variable macros depending on the presence of tokens in the variable argument.
  • Support for "" operator for three-way comparison.
  • Support for default element initializers for bitfields.
  • Possibility of lambda capture of "*this" expressions.

    struct int_value {
    int n = 0;
    auto getter_fn() {
    //BAD:
    // return [=]() { return n; };

    // GOOD:
    return [=, *this]() { return n; };
    }
    };

  • Calling elements by pointer (Pointer-to-member), using pointers to temporary objects defined through the "const &" expression.
  • The delete operator with a destructor described in the document P0722R1.
  • Classes are allowed to use template parameters without a type.

    struct foo {
    foo() = default;
    constexpr foo(int) {}
    };

    template
    auto get_foo() {
    return f;
    }

    getfoo(); // uses implicit constructor
    get_foo ();

  • Non-preserved lambda expressions with a constructor.
  • Validity of template syntax for lambda expressions ("auto f = [] (std::vector v)").
  • Ability to use string literals in template parameters.
  • Support for C-style initialization syntax - fields not explicitly listed in the initialization list are initialized by default.

    struct A {
    int x;
    int y;
    int z = 123;
    };

    A a {.x = 1, .z = 2}; // ax == 1, ay == 0, az == 2

  • Support for empty members of data structures.
  • Support for likely and unlikely attributes to inform the optimizer about the probability of triggering a conditional construct (β€œ[[likely]] if (random > 0) {β€œ).
  • Ability to use ranges to initialize variable values ​​in a "for" loop

    for (auto v = std::vector{1, 2, 3}; auto& e : v) {

  • Automatic calculation of array size in new ("new double[]{1,2,3}");
  • Attribute "[[no_unique_address]]" in which variables without data do not take up space.
  • Atomic pointers (std::atomic > and std::atomic >).
  • Ability to call virtual functions in conditional expressions.
  • Support for quick (immediate) functions that can only work with constants.

    consteval int sqr(int n) {
    return n*n;
    }

    constexpr int r = sqr(100); // OK
    int x = 100;
    int r2 = sqr(x); // ERROR: 'x' cannot be used as a constant

  • Ability to use constexpr with virtual functions ("constexpr virtual int f() const { return 2; }").
  • In the standard library:
    • Added support for the char8_t type for UTF-8 strings.
    • Added header files bit (bit operations) and version.
    • Now it is possible to check the prefix and suffix of strings (starts_with, ends_with).
    • Added std::remove_cvref, std::unwrap_reference, std::unwrap_decay_ref, std::is_nothrow_convertible and std::type_identity traits.
    • Added functions std::midpoint, std::lerp, std::bind_front, std::source_location, std::visit, std::is_constant_evaluated and std::assume_aligned.
    • Support for arrays has been added to std::make_shared.
    • Added std::to_array function to convert array-like objects to std::array.
  • More convenient enum syntax:

    enum class rgba_color_channel { red, green, blue, alpha };

    std::string_view to_string(rgba_color_channel my_channel) {
    switch(my_channel) {
    using enum rgba_color_channel;
    case red: return "red";
    case green: return "green";
    case blue: return "blue";
    case alpha: return "alpha";
    }
    }

  • In indexes, due to undefined behavior, the use of the "," ("a[b,c]" operation) is prohibited. Support for most operations on variables declared with the volatile keyword has been dropped, including the operations "++" and "-" with standard types are prohibited.
  • Reduced the number of situations in which "typename" is required to indicate the presence of a type.

Source: opennet.ru

Add a comment