C++23特性一览

New language features

New language feature testing macros

Explicit object parameters, explicit object member functions (deducing this)

if consteval / if not consteval

Multidimensional subscript operator (e.g. v[1, 3, 7] = 42;)

static operator()

static operator[]

auto(x): decay-copy in the language

Attributes on lambda-expressions

Optional extended floating-point types: std::float{16|32|64|128}_t, and std::bfloat16_t.

#elifdef, #elifndef, and #warning

Literal suffix 'Z'/'z' for (signed) std::size_t literals

Assumptions via new attribute [[assume(expression)]]

Named universal character escapes

A portable source file encoding is UTF-8

White-spaces trimming before line splicing.

New library features

New modules

std and std.compat

New headers

C compatibility headers:

Library features

New library feature testing macros

New ranges fold algorithms

String formatting improvements

"flat" container adaptors: std::flat_map, std::flat_multimap, std::flat_set, std::flat_multiset

std::mdspan

std::generator

std::basic_string::contains, std::basic_string_view::contains

Construction of std::string_view from nullptr is disallowed

std::basic_string::resize_and_overwrite

Monadic operations for std::optional: or_else, and_then, transform

Stacktrace library

New ranges algorithms:

New range adaptors (views):

Changes to ranges library

Changes to views library

Marking unreachable code: std::unreachable

New vocabulary type std::expected

std::move_only_function

New I/O stream std::spanstream with program-provided fixed size buffer

std::byteswap

std::to_underlying

Associative containers heterogeneous erasure.


C++23, the latest iteration of the C++ programming language standard, follows its predecessor C++20 with several enhancements aimed at improving usability, performance, and code clarity. As a refinement of the language, C++23 doesn't introduce as many large features as C++20, which included major additions like concepts, ranges, and coroutines. However, it does provide important updates and new features that developers should be aware of. This article delves into some of the most significant changes and improvements in C++23 compared to previous versions.

  1. Simplified Syntax for using Declarations
    C++23 has introduced a simplified syntax for using declarations, which allows a base class's constructor or assignment operator to be inherited more concisely. Previously, inheriting constructors from a base class required specifying each constructor individually, which could be verbose and error-prone. The new feature reduces boilerplate code and enhances readability.
C++ 复制代码
struct Base {
    Base(int x) {}
    Base(double x, int y) {}
};

struct Derived : Base {
    using Base::Base;  // Inherit all constructors from Base
};
  1. Standardized std::print
    One of the most anticipated features in C++23 is the introduction of std::print. This feature aims to standardize a common operation---printing to the console---which previously required using std::cout or other more complex I/O functions. std::print simplifies this task by providing a straightforward way to print formatted data, reducing the learning curve for new C++ programmers and increasing code clarity.
C++ 复制代码
#include <iostream>

int main() {
    std::print("Hello, World!\n");
    std::print("The answer is: {}\n", 42);
}
  1. Enhancements to std::expected
    std::expected is a template class that handles expected errors as an alternative to exceptions. It was proposed to provide a way to return and check for error conditions in a manner that is more visible in the function signature than exceptions. C++23 aims to include this feature to simplify error handling and improve performance by avoiding the overhead associated with exceptions.
C++ 复制代码
#include <expected>
#include <iostream>

std::expected<int, const char*> compute(int x) {
    if (x < 0) return "Negative input not allowed";
    return x * x;
}

int main() {
    auto result = compute(10);
    if (result) {
        std::cout << "Result: " << *result << std::endl;
    } else {
        std::cout << "Error: " << result.error() << std::endl;
    }
}
  1. std::mdspan
    Another notable addition is std::mdspan a multidimensional array view that provides an interface for accessing elements in a multidimensional data layout without owning the underlying data. This feature is crucial for scientific and numerical applications where performance and flexibility in data access patterns are important.
C++ 复制代码
#include <mdspan>
#include <vector>
#include <iostream>

int main() {
    std::vector<int> data = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    std::mdspan<int, std::extents<3, 3>> matrix(data.data());

    for (int i = 0; i < 3; ++i) {
        for (int j = 0; j < 3; ++j) {
            std::cout << matrix(i, j) << " ";
        }
        std::cout << "\n";
    }
}
  1. Improved Lambda Expressions
    C++23 continues to enhance lambda expressions by introducing new features such as simplified syntax for not capturing any variables and improved constexpr capabilities. These improvements make lambdas more powerful and easier to use in a wider range of contexts, including compile-time programming.
C++ 复制代码
#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> numbers = {1, 2, 3, 4, 5};
    auto is_even = [](int x) constexpr { return x % 2 == 0; };
    auto pos = std::find_if(numbers.begin(), numbers.end(), is_even);

    if (pos != numbers.end()) {
        std::cout << "First even number: " << *pos << std::endl;
    }
}
  1. constexpr Enhancements
    The constexpr specifier has been significantly expanded in C++23, allowing more complex computations to be performed at compile time. This includes the ability to use dynamic memory allocation, try and catch blocks, and virtual calls within constexpr functions, which were previously not possible.
C++ 复制代码
#include <iostream>

constexpr int* create_array(int size) {
    return new int[size]{1, 2, 3};  // Dynamic allocation in constexpr
}

int main() {
    constexpr auto* array = create_array(3);
    std::cout << array[0] << ", " << array[1] << ", " << array[2] << std::endl;
    delete[] array;
}
  1. Networking TS (Technical Specification)
    While not fully integrated into the standard, progress has been made on the Networking TS, which is expected to eventually be part of the standard library. C++23 has made strides towards incorporating more networking capabilities directly into the standard library, facilitating easier and more standardized network programming.
    Conclusion
    C++23 offers several practical enhancements that refine the capabilities introduced in C++20 and earlier versions. As with any new standard, the adoption rate will depend on compiler support and the specific needs of projects. Nonetheless, personally I find these improvements in C++23 an attractive upgrade for C++ developers seeking to leverage the latest advancements in the language.
相关推荐
zengy52 分钟前
代码随想录打卡第十三天
数据结构·c++·算法·leetcode
m0_7471245317 分钟前
将QT移植到IMX6ULL开发板
linux·单片机·qt·imx6ull
起个别名24 分钟前
必须掌握的Linux的九大命令
linux·服务器·网络
Danica~37 分钟前
RpcChannel的调用过程
网络·c++·rpc
结衣结衣.42 分钟前
完全理解C语言函数
java·linux·c语言·数据库·经验分享·笔记
零K沁雪43 分钟前
VirtualBox 安装 Ubuntu Server24.04
linux·运维·ubuntu
Hundreds_N1 小时前
kali改回官方源后更新失败
linux·kali
wenhui.wang1 小时前
Linux/Ubuntu访问局域网共享文件夹
linux·ubuntu
C_player_0011 小时前
C++ list的模拟实现
c++
光亮§那方2 小时前
linux - cp 命令
linux·ubuntu