C++23 Features

Latest additions to C++

Interview Relevant: Cutting-edge features

C++23 Features

C++23 completes C++20 and adds modern conveniences.

New Features

  • std::print: Type-safe, formatted printing (faster than iostreams).
  • Deducing this: Explicit object parameter for recursive lambdas.
  • std::expected: Error handling without exceptions.
  • Multidimensional Subscript: m[1, 2].

Code Examples

C++23 latest features and improvements.

cpp
1// std::print (finally!)
2print("Hello, {}!\n", "World");
3println("Value: {}", 42);
4
5// Deducing this
6struct Counter {
7    int value = 0;
8    auto& increment(this auto& self) {
9        self.value++;
10        return self;
11    }
12};
13
14// if consteval
15constexpr int compute(int x) {
16    if consteval {
17        return x * x;  // Compile-time
18    } else {
19        return x + x;  // Runtime
20    }
21}
22
23// std::expected (error handling)
24expected<int, string> divide(int a, int b) {
25    if (b == 0) return unexpected("Division by zero");
26    return a / b;
27}
28
29// Multidimensional subscript operator
30auto& operator[](int x, int y, int z) {
31    return data[x][y][z];
32}
33matrix[1, 2, 3] = 42;
34
35// std::mdspan (multidimensional view)
36mdspan<int, extents<3, 4>> matrix(data);
37
38// Ranges improvements
39auto chunks = vec | views::chunk(3);
40auto slides = vec | views::slide(2);
41
42// Stacktrace library
43auto trace = stacktrace::current();

AI Tutor

Ask about the topic

Sign in Required

Please sign in to use the AI tutor

Sign In