Skip to content

The Latest Standard (C++23)

C++23 refines the revolution started by C++20, filling in gaps and adding powerful new library features.

std::print and std::println

Finally, a modern way to print to the console! It combines the performance of printf with the safety and ease of std::format.

1
2
3
4
5
6
7
8
#include <print>

int main() {
    std::println("Hello, {}!", "World");

    int x = 42;
    std::print("The value is {}\n", x);
}

std::expected (Error Handling)

std::expected<T, E> represents either a value of type T (success) or an error of type E (failure). It is cleaner than exceptions for expected errors.

#include <expected>

enum class Error { DivideByZero };

std::expected<int, Error> divide(int a, int b) {
    if (b == 0) return std::unexpected(Error::DivideByZero);
    return a / b;
}

auto result = divide(10, 0);
if (result) {
    std::println("Result: {}", *result);
} else {
    std::println("Error occurred");
}

Deducing this (Explicit Object Parameter)

Allows you to deduce the value category (lvalue/rvalue) and const-ness of the object (this) in a member function. Useful for CRTP and deduplicating code.

1
2
3
4
5
6
struct Button {
    void click(this auto&& self) {
        // self is 'Button&', 'const Button&', or 'Button&&'
        std::println("Clicked!");
    }
};

if consteval

A safer version of if (std::is_constant_evaluated()). It executes the block only during compile-time evaluation.

1
2
3
4
5
6
7
8
9
consteval int strict_compile_time() { return 42; }

constexpr int func(int x) {
    if consteval {
        return strict_compile_time(); // OK
    } else {
        return x; // Runtime path
    }
}

Multidimensional Arrays (std::mdspan)

A non-owning, multidimensional view of contiguous data. Great for scientific computing and image processing.

#include <mdspan>
#include <vector>

int main() {
    std::vector<int> data(12); // 1D storage
    // View as 3x4 matrix
    auto matrix = std::mdspan(data.data(), 3, 4);

    matrix[1, 2] = 5; // Access using (row, col)
}

Standard Library Modules (import std;)

C++23 standardizes the std module, allowing you to import the entire standard library at once with minimal compile-time cost.

1
2
3
4
5
6
import std;

int main() {
    std::vector<int> v = {1, 2, 3};
    std::println("Size: {}", v.size());
}

Other Features

  • std::unreachable(): Marks code as unreachable (optimization hint).
  • std::to_underlying(): Casts an enum to its underlying integer type.
  • std::byteswap(): Reverses endianness.