Open In App

C++ 23 Standard

Last Updated : 25 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

C++23, officially known as ISO/IEC 14882:2023, is the latest standardized version of the C++ programming language, published in 2023. As C++ introduces new improvements and features every 3 years in the form of a standard like C++20 introduced powerful features such as concepts, ranges, and coroutines, marking a substantial leap in expressiveness and power. C++17 brought features like structured bindings, inline variables, and fold expressions. Each standard has progressively enhanced the language's capabilities, and C++23 continues this trend with a range of new features and improvements that we will discuss in this article.

In this article, we will discuss the various features introduced in C++23, along with enhancements to the standard library and compiler support. We will also examine the impact of these changes on the C++ community.

Added Features and Improvements in the C++ 23 Standard

C++23 brings several significant changes and additions to the language, enhancing its usability, performance, and expressiveness. Some of the important features include:

  • Explicit Object Parameters (deducing this): Explicit object parameters allow specifying the object parameter explicitly in a member function, making the code more readable and maintaining consistent semantics.
  • if consteval and if not consteval: These constructs help in compile-time evaluations, allowing code to differentiate between compile-time and runtime contexts.
  • Multidimensional Subscript Operator: The multidimensional subscript operator (e.g., v[1, 3, 7] = 42;) provides a more intuitive way to handle multidimensional data structures.
  • Static Operators: C++ 23 introduces static operator() and static operator[], enabling static member functions to be called like normal operators.
  • Attributes on Lambda Expressions: Adding attributes to lambda expressions in C++23 allows developers to specify additional information and constraints directly within lambda expressions.
  • Extended Floating-Point Types: C++ 23 introduces optional extended floating-point types, such as std::float{16|32|64|128}_t and std::bfloat16_t, providing more precision options for numerical computations.
  • Preprocessor Enhancements: New preprocessor directives like #elifdef, #elifndef, and #warning improve the clarity and flexibility of conditional compilation.
  • Named Universal Character Escapes: Named universal character escapes make it easier to include non-ASCII characters in source code.
  • UTF-8 Source File Encoding: C++ 23 mandates UTF-8 as a portable source file encoding, ensuring consistent character representation across different platforms.
  • White-Space Trimming Before Line Splicing: This feature trims white spaces before line splicing, improving code readability and maintenance.

New Modules in C++ 23

C++ 23 introduces two new modules that improve modularity and integration with existing C++ codebases.

  • std
  • std.compat

New Headers in C++ 23

C++ 23 adds several new headers to the standard library, each providing unique functionalities:

Library Features in C++ 23

New library feature testing macros and enhancements include:

  • Ranges Fold Algorithms: New algorithms to work with ranges.
  • String Formatting Improvements: Enhancements in string formatting.
  • Flat Container Adaptors: Introduction of std::flat_map, std::flat_multimap, std::flat_set, std::flat_multiset.
  • std::mdspan: For multi-dimensional span support.
  • std::generator: To facilitate generator functions.
  • New String Methods: std::basic_string::contains and std::basic_string_view::contains.
  • Disallowed Construction: Disallows construction of std::string_view from nullptr.
  • std::basic_string::resize_and_overwrite: New method for resizing and overwriting strings.
  • Monadic Operations: For std::optional, including or_else, and_then, transform.
  • Stacktrace Library: New library for handling stack traces.
  • New Ranges Algorithms: Additional algorithms for working with ranges.
  • New Range Adaptors: New adaptors for views.
  • Marking Unreachable Code: Using std::unreachable.
  • New Vocabulary Type: std::expected for handling expected values and errors.
  • std::move_only_function: A new move-only function wrapper.
  • New I/O Stream: std::spanstream with a program-provided fixed size buffer.
  • Utility Functions: std::byteswap and std::to_underlying.
  • Heterogeneous Erasure: In associative containers.

Standard Library Enhancements in C++ 23

C++23 includes numerous additions and improvements to the standard library, making it more comprehensive and efficient.

  • Library TS Additions: Technical Specifications (TS) such as ranges and networking have been integrated into the standard library, providing more robust and versatile components for developers.
  • Standard Library Enhancements: Enhancements to existing library components, including containers, algorithms, and iterators, have been made to improve performance and usability.

Deprecated Features in the C++23 Standard

While C++23 introduces many new features and improvements, it also deprecates some existing features to encourage better programming practices and prepare for future changes. Deprecation means that while these features are still available, their use is discouraged, and they may be removed in future standards. Some of the deprecated features in C++23 include:

Feature

Description

std::auto_ptr

The std::auto_ptr class template, which was already deprecated in C++11, remains deprecated. Developers are encouraged to use std::unique_ptr or std::shared_ptr for better memory management.

std::allocator<void>

The std::allocator<void> specialization, which provided a way to create allocators for void pointers, is deprecated. Modern C++ practices have rendered this specialization unnecessary.

Function Try Blocks

Function try blocks, which allow exception handling around an entire function body, are deprecated. Developers are encouraged to use standard try-catch blocks within the function instead.

Old-style Casts

The use of old-style C casts (e.g., (int)x) is deprecated in favor of C++-style casts (static_cast, dynamic_cast, const_cast, and reinterpret_cast) which provide better type safety and clarity.

Implicit Capture of this in Lambda Expressions

Implicit capture of this by value in lambda expressions is deprecated. Capturing this explicitly or using [=] for by-value capture is encouraged for better clarity and safety.

Standard Library Functions

Some standard library functions, such as std::strstream and std::random_shuffle, have been deprecated in favor of safer or more efficient alternatives like std::stringstream and std::shuffle.

Compiler Support and Compatibility

Major compilers like GCC, Clang, and MSVC are actively working on providing full support for C++23 features. Each new release brings more comprehensive implementation and better optimization for C++23.

GCC (GNU Compiler Collection)

GCC has led the way in introducing new C++ standards. Versions above GCC 12.1 offers comprehensive support for C++23 features. The GCC team is continuously working towards full compliance to ensure developers can take advantage of the latest language improvements.

g++ -std=c++23 main.cpp -o main

Clang

Clang, another popular compiler, offers robust support for C++23. Clang's dynamic development community and modular architecture allows the quick adoption of new standards.

clang++ -std=c++23 main.cpp -o main

MSVC (Microsoft Visual C++)

Microsoft's Visual C++ compiler (MSVC) has historically supported new C++ standards swiftly. The latest versions of MSVC, integrated into the Visual Studio IDE, include full support for C++23.

cl /std:c++23 main.cpp

Intel C++ Compiler

Known for its performance optimizations, the Intel C++ Compiler supports C++23, making it suitable for high-performance applications.

Borland C++ Compiler

Borland's C++ compiler (Embarcadero C++Builder) also supports C++23, maintaining its relevance in modern development environments.

Conclusion

C++23 introduces a range of new language features and library enhancements aimed at making C++ more efficient and easier to use. Key improvements include modules, concept enhancements, coroutine updates, and a richer standard library. As the C++ community continues to embrace the new standard, C++23 is expected to play a crucial role in the development of modern applications, driving innovation and setting the stage for future advancements in the language.


Next Article
Article Tags :
Practice Tags :

Similar Reads