ISO Committee approved "C ++ 20" standard





Recently, the ISO Committee for Standardization of the C ++ Language (yes, there is one) approved the international standard "C ++ 20". The features presented in the specification are supported by the GCC, Clang, and Microsoft Visual C ++ compilers. In addition, standard libraries with support for "C ++ 20" are implemented within the Boost project.



The next step is preparing the document for publication. Then, in early November, the final version will be sent to ISO, after which it will be published under the formal name ISO / IEC 14882: 2020. The committee is already working on the next standard, C ++ 23 (C ++ 2b). Under the cut - features of "C ++ 20" with code examples.



What's new?



The committee added "concepts" , template extensions that allow you to define a set of requirements for template parameters. At compile time, they constrain the set of arguments that can be accepted as template parameters. The concepts make it possible to avoid logical inconsistencies between the properties of the data types that are used inside the template and the properties of the data types of the input parameters.



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


Accepted extension to work with the modules. They can be used instead of header files. Modules provide a new way of organizing sources based on the definition of component boundaries, without including header files using "#include".



Added __VA_OPT__ macro for adaptive expansion of variadic macros depending on the presence of tokens in the variadic argument.



Added support for the <=> operator for three-way comparison. Default element



initializers for bit fields are supported .



Added the ability to capture lambda expressions * this.



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


Classes can now use typeless template parameters.



   struct foo {
     foo() = default;
     constexpr foo(int) {}
   };
 
   template <foo f>
   auto get_foo() {
     return f; 
   }
 
   get_foo(); // uses implicit constructor
   get_foo<foo{123}>();


Now you can use string literals in template parameters. C-style initialization syntax is supported. Those that are not explicitly listed in the field initialization lists 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


Empty members of data structures are supported.



The likely and unlikely attributes are supported to inform the optimizer about the probability of the conditional statement being triggered ("[[likely]] if (random> 0) {").



Now you can use ranges to initialize variable values ​​in a for loop



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


Immediate functions are supported , which 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'     


Added to the library:

  • support for char8_t type for UTF-8 strings.
  • headers bit (bit operations) and version.
  • the ability to check the prefix and suffix of strings (starts_with, ends_with).
  • traits std :: remove_cvref, std :: unwrap_reference, std :: unwrap_decay_ref, std :: is_nothrow_convertible, and std :: type_identity.
  • the functions std :: midpoint, std :: lerp, std :: bind_front, std :: source_location, std :: visit, std :: is_constant_evaluated, and std :: assume_aligned.
  • support for arrays in std :: make_shared.
  • std :: to_array function for converting array-like objects to std :: array.


The enumeration syntax is now more convenient:

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


It is forbidden to use the operations "," ("a [b, c]") in indexes . Most operations on variables declared with the violate keyword, including the forbidden ++ and - operations on standard types, are not supported.






All Articles