The C++26 standard has been approved

The ISO committee for the C++ language standard has finalized the specification that forms the international standard "C++26". The features presented in the specification are partially supported in the compilers GCC, Clang, and Microsoft Visual C++. Libraries that support C++26 are implemented within the Boost project.

In the next two months, the approved specification will undergo document preparation for publication, during which editorial corrections for spelling errors and typos will be made. In early November, the resulting document will be sent to ISO for publication under the formal name ISO/IEC 14882:2026.

Key features of C++26:

  • Elements of contract programming (Contracts) have been implemented, allowing the definition of formal specifications for interfaces using three new operators: pre (precondition), post (postcondition), and contract_assert (assertion check). The "pre" operator defines conditions that must be met before a function call (input validation); "post" defines conditions that must hold after execution (output requirements); contract_assert indicates when exceptions may arise. This feature will appear in GCC 16. int f(const int x) pre (x != 1) // input requirements post (r : r == x && r != 2) // output requirements; r is the result value { contract_assert (x != 3); return x; }
  • Support for reflection has been added, allowing tracking and modifying program elements at compile time. New operators "^^" have been introduced for obtaining meta-information about grammatical constructs, and "[:…:]" for performing reverse transformation. For transforming and processing the information obtained during inspection, the std::meta library has been proposed, enabling features such as computations with constants. Reflection support will be added in GCC 16. constexpr int i = 42, j = 42; constexpr std::meta::info r = ^^i, s = ^^i; static_assert(r == r && r == s); static_assert(^^i != ^^j); // ‘i’ and ‘j’ have different values. static_assert(constant_of(^^i) == constant_of(^^j)); // ‘i’ and ‘j’ are the same static_assert(^^i != std::meta::reflect_constant(42)); // differs from the value 42
  • A "template for" operator has been added for iterating over elements such as parameter packs, tuple-like objects, and reflection results (meta-objects) at compile time in a traditional loop style. When executing "template for," the loop body is expanded for each element, and each iteration is processed in a separate scope, where the loop variable is constant. In the context of reflection, "template for" can be applied to iterate over properties of classes or enumerations. This feature will appear in GCC 16. void f() { template for (constexpr int I : std::array{1, 2, 3}) { static_assert(I < 4); } } will be expanded to: void f() { { constexpr auto&& __range = std::array{1, 2, 3}; constexpr auto __begin = __range.begin(); constexpr auto __expansion_size = __range.end() - __begin; // 3 { constexpr int I = *(__begin + 0); static_assert(I < 4); } { constexpr int I = *(__begin + 1); static_assert(I < 4); } { constexpr int I = *(__begin + 2); static_assert(I < 4); } } }
  • A std::execution framework has been added for asynchronous and parallel execution of code. It provides the scheduler object, defining the job execution scheduler (thread, thread pool, GPU, event loop), sender, defining the work to be executed, and receiver — a result handler. using namespace std::execution; scheduler auto sch = thread_pool.scheduler(); sender auto begin = schedule(sch); sender auto hi = then(begin, []{ std::cout << "Hello world! Have an int."; return 13; }); sender auto add_42 = then(hi, [](int arg) { return arg + 42; }); auto [i] = this_thread::sync_wait(add_42).value();
  • A std::simd library has been added to parallelize execution of operations on data using SIMD instruction sets, such as AVX-512 and NEON, utilizing the standard C++ type system. std::simd a = {1.0f, 2.0f, 3.0f, 4.0f}; std::simd b = {5.0f, 6.0f, 7.0f, 8.0f}; std::simd result = a + b;
  • A variable-sized vector (array) implementation std::inplace_vector has been proposed, which is allocated on the stack and whose size is determined at compile time. The API is close to std::vector, but the array elements are stored not in the "heap" but inside the object. inplace_vector a(10); inplace_vector b(std::move(a)); assert(a.size() == 10);
  • The directive "#embed" has been added for embedding binary resources into the code. const unsigned char icon_display_data[] = { #embed "art.png" };
  • Support has been added for generating and handling exceptions at compile time in the context of constexpr. constexpr std::optional checked_divide(unsigned n, unsigned d) { try { return divide(n, d); } catch (…) { return std::nullopt; } } constexpr date parse_date(std::string_view input) { auto [correct, year, month, day] = ctre::match(input); if (!correct) { throw incorrect_date{input}; } return build_date(year, month, day); }
  • A data structure std::hive has been implemented for unordered data storage and for reusing memory freed after deleted elements. This structure is optimized for workloads with high addition and deletion intensity in arbitrary order. Unlike arrays, deleting an element in std::hive does not shift other elements; instead, it marks the deleted element as empty, with the freed position being filled when a new element is added.
  • The std::linalg library has been added with an API for linear algebra based on BLAS.
  • Support has been added for the Hazard Pointer synchronization mechanism, which prevents memory deallocation of objects that are still being accessed by other threads without using locks. When an object is deleted, it is merely marked as deleted, and the memory occupied by the object is only freed once all threads have removed the hazard pointer set during work with the object.
  • Support has been added for the RCU (Read-Copy Update) synchronization mechanism — during write operations, a new instance of the object is created, and read operations are not blocked; they continue to work with the old instance. Once the modification is complete, the new instance becomes active and new reading operations are performed with it, while the old instance is deleted after all reading threads have finished.
  • Changes have been made to enhance the security of the standard library, such as checks for permissible values and buffer overflow. For example, when accessing the element "constexpr reference operator[](size_type idx) const;", a check is added for the condition "idx < size()".
  • The ability to use the keyword "constexpr" with the placement new operator has been provided for allocating an object in pre-allocated memory at compile time.
  • Support for structured bindings has been added in the context of 'constexpr', meaning that references to constant expressions can now themselves be constant expressions. Support is implemented for arrays and simple structures. constexpr int arr[] = {1, 2}; constexpr auto [x, y] = arr;
  • Structured bindings now include the ability to use the syntax '...' to indicate packs that capture the remaining number of elements from the assigned sequence. auto [x,y,z] = f(); // the variables x, y, z will store three elements returned by f(). auto […xs] = f(); // the pack xs will store all elements returned by f(). auto [x, …rest] = f(); // x will store the first element, and rest will store the remaining. auto [x, y, …rest] = f(); // x will store the first element, y will store the second, and rest will store the third. auto [x, …rest, z] = f(); // x will have the first, rest will have the second, and z will have the third.
  • Support for 'trivial relocatability' of types has been added, enabling the optimization of object movements of a given type through cloning them in memory without invoking constructors or destructors. Properties memberwise_trivially_relocatable and memberwise_replaceable are implemented for classes, and functions trivially_relocate_at and trivially_relocate have been added for low-level relocation of one or more objects.
  • Support for binding the main() function to the global module and defining the main() function in named modules has been implemented.
  • A variadic 'friend' operator ('friend Ts…') has been added.
  • Attributes for structured bindings have been implemented;
  • The syntax '= delete("reason")' has been added.
  • "@", "$", and "`" have been included in the basic set of characters.
  • The use of structured bindings as a condition in if and switch statements has been made possible.
  • It is now possible to use multiple placeholder variables named '_' in the same scope, for example, the following constructs are now valid: struct S { int _, _; }; void func() { int _, _; } void other() { int _; // a warning was previously issued in -Wunused mode }
  • String literals can now be used in contexts where they are not used to initialize character arrays, do not appear in the resulting code, and are only applied during compilation for diagnostic messages and preprocessing, such as in directive parameters and attributes like _Pragma, asm, extern, static_assert, [[deprecated]], and [[nodiscard]].
  • New built-in functions added: ‘__builtin_is_within_lifetime’ to check the activity of alternatives in unions and ‘__builtin_is_virtual_base_of’ to check if a base class is virtual.
  • Trivial infinite loops have been implemented without undefined behavior.
  • An error is now reported when deleting a pointer to an incomplete type.
  • The syntax for defining variadic parameters with ellipsis without a preceding comma has been deprecated (for example, specifying "void e(int…)" instead of "void e(int, …)").
  • The use of macros to declare modules has been prohibited.
  • Implicit conversions of enumerated values in arithmetic calculations have been deprecated. int main() { enum E1 { e }; enum E2 { f }; bool b = e <= 3.7; // deprecated int k = f - e; // deprecated int x = +f - e; // OK }
  • Support for direct comparison of arrays has been discontinued. int arr1[5]; int arr2[5]; bool same = arr1 == arr2;
  • The template class is_trivial has been deprecated.

    Source: opennet.ru
Buy reliable website hosting with DDoS protection, VPS VDS servers 🔥 Buy reliable website hosting with DDoS protection, VPS VDS servers | ProHoster