C++20 standard approved

ISO Committee for C++ Language Standardization approved international standard "C++20". The capabilities presented in the specification, except for isolated cases, supported in compilers GCC, Clang and Microsoft Visual C++. Standard libraries supporting C++20 are implemented as part of the Boost.

In the next two months, the approved specification will undergo document preparation for publication, during which editorial corrections of typographical errors and mistakes will be made. In early November, the resulting version of the document will be sent to ISO for publication under the formal name ISO/IEC 14882:2020. Meanwhile, the committee has already begun work on the next standard C++23 (C++2b) and will consider possible innovations.

Key Features C++20 (code examples):

  • ‘Concepts’ have been added, extending templates to define a set of requirements for template parameters that restrict the set of arguments that can be accepted as template parameters during compilation. Concepts can be applied to avoid logical inconsistencies between the properties of data types used within the template and the properties of the data types of input parameters.

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

  • Included are extension modules for working with modules, which can be used instead of header files. Modules provide a new way to organize source texts based on defining component boundaries, without relying on "#include" header files.
  • The macro __VA_OPT__ for adaptive expansion of variadic macros depending on the presence of tokens in the variadic argument.
  • Support for the operator "" for three-way comparison.
  • Support for default element initializers for bit-fields.
  • The ability to lambda-capture the expression "*this".

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

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

  • Calling elements by pointer-to-member, using pointers to temporary objects defined by the expression "const &".
  • 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;
    }

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

  • Non-storing lambda expressions with constructors.
  • The validity of using template syntax for lambda expressions ("auto f = [](std::vector v)").
  • The ability to use string literals in template parameters.
  • Support for C-style initialization syntax — explicitly non-listed fields in an initializer list are initialized by default.

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

    A a {.x = 1, .z = 2}; // a.x == 1, a.y == 0, a.z == 2

  • Support for empty members in data structures.
  • Support for likely and unlikely attributes to inform the optimizer about the probability of conditional constructs being executed ("[[likely]] if (random > 0) {").
  • The ability to use ranges for initializing variable values in the "for" loop.

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

  • Automatic array size computation in new ("new double[]{1,2,3}");
  • The "[[no_unique_address]]" attribute where variables without data do not occupy space.
  • Atomic pointers (std::atomic<shared_ptr> and std::atomic<weak_ptr>).
  • The ability to call virtual functions in conditional expressions.
  • Support for immediate functions that can only operate on 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

  • The ability to apply constexpr with virtual functions ("constexpr virtual int f() const { return 2; }").
  • In the standard library:
    • Support for the char8_t type for UTF-8 strings has been added.
    • Header files for bit (bitwise operations) and version have been added.
    • The ability to check string prefixes and suffixes (starts_with, ends_with).
    • Type traits std::remove_cvref, std::unwrap_reference, std::unwrap_decay_ref, std::is_nothrow_convertible, and std::type_identity have been added.
    • Functions std::midpoint, std::lerp, std::bind_front, std::source_location, std::visit, std::is_constant_evaluated, and std::assume_aligned have been added.
    • Support for arrays has been added to std::make_shared.
    • The std::to_array function has been added for converting array-like objects into std::array.
  • A more convenient syntax for enumerations:

    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";
    }
    }

  • The use of the operation "," ("a[b,c]") is prohibited in indices due to undefined behavior. Support for most operations with variables declared with the volatile keyword has been discontinued, including the prohibitions on "+" and "–" operations with standard types.
  • The number of situations requiring the specification of "typename" to indicate the presence of a type has been reduced.

Source: opennet.ru

Buy reliable website hosting with DDoS protection, VPS VDS servers 🔥 Buy reliable website hosting with DDoS protection, VPS VDS servers | ProHoster