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.
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 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, 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
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.
Similar Reads
C++ 03 Standard
The C++ programming language, initially developed by Bjarne Stroustrup in 1983, quickly became one of the most popular programming languages due to its efficiency, flexibility, and support for object-oriented programming. However, as the language evolved, variations and extensions introduced by diff
4 min read
C 17 Standard
C 17 standard, officially known as ISO/IEC 9899:2018, is the most recent revision of the C programming language standard, it was finalized in 2017 and published in June 2018. It is often referred to as C 18 due to its publication year, this standard is a direct successor to C11 (ISO/IEC 9899:2011) a
3 min read
C++ 11 Standard
C++ 11, officially known as ISO/IEC 14882:2011, is a significant version of the C++ programming language standard, published in 2011. It marked a major fix up of the language, introducing various features and enhancements that improved the usability, performance, and safety of C++ code. Before C++ 1
5 min read
C++ 98 Standard
C++ 98, officially known as ISO/IEC 14882:1998, was the first international standard for the C++ programming language, published in 1998. This version standardized features that were already widely used in the C++ community and also introduced some new features that laid the foundation for future ad
4 min read
ANSI C - C89 Standard
The C programming language, developed in the early 1970s by Dennis Ritchie at Bell Labs, quickly gained popularity due to its efficiency, portability, and flexibility. However, C variations and extensions by different compiler vendors led to compatibility issues. To address this, the American Nation
5 min read
C++ Standards and Implementations
C++ programming language is widely used and known for its power, versatility, and performance. C++ is an extension of the C programming language created by Danish computer scientist Bjarne Stroustrup. With time several C++ standards have been introduced with new features and enhancements. In this ar
6 min read
C++23 <print> Header
C++ has long been a powerful language, known for its versatility and performance. With each new standard release, the language evolves, introducing features that enhance developer productivity and code readability. One of these additions of the C++ 23 standard is the introduction of <print> he
3 min read
std::cbrt() in C++
The std::cbrt() is an inbuilt function in C++ STL which is used to calculate the cube root of number. It accepts a number as argument and returns the cube root of that number.Syntax: // Returns cube root num (num can be // of type int, double, long double or // long long type. // The return type is
2 min read
C++ 11 - <cstdint> Header
<cstdint> header in C++ ensures the portability of integer types with specialized width and signedness across various systems. The absence of standardization for integer types caused issues in coding and constructing portable programs before the advent of. In this article, we will explore the
3 min read
C++ 20 - <numbers> Header
C++20 has a recently developed header file labeled that incorporates mathematical functions and constants. Its purpose is to provide standard library support for mathematical operations, simplifying the process for C++ programmers to incorporate mathematical functions and constants into their progra
3 min read