Three Practical C++23 Features You’ll Use Frequently
The article introduces three useful C++23 additions—a literal suffix for size_t and ptrdiff_t that ensures portable index types, a multidimensional subscript operator enabling concise matrix element access, and a std::string/std::string_view contains() function that simplifies substring checks—each likely to see frequent use.
size_t and ptrdiff_t literal suffixes
In C++23, the built‑in types std::size_t (an unsigned integer capable of representing the size of any object) and std::ptrdiff_t (a signed integer representing the result of pointer subtraction) gain dedicated literal suffixes. The suffixes uz, uZ, Uz or UZ deduce std::size_t, while z or Z deduce std::ptrdiff_t. This allows code such as:
auto a = 42uz; // a is std::size_t
auto b = -42z; // b is std::ptrdiff_tThese literals avoid the common 32‑bit/64‑bit mismatch when using plain unsigned int for indices. For example, a loop written with auto i = 0u compiles on both 32‑bit and 64‑bit platforms, but the literal 0uz guarantees the loop variable matches the return type of size() on any architecture.
Multidimensional subscript operator
Standard one‑dimensional containers are accessed with operator[], but multidimensional containers lack a natural syntax; writing arr[0, 1, 2] is illegal. Common work‑arounds include a variadic at() function, overloading operator(), using a braced‑list overload, or chaining single‑parameter subscripts.
To demonstrate a cleaner approach, the article defines a simple matrix class:
template<typename T, size_t R, size_t C>
struct matrix {
T& operator()(size_t const r, size_t const c) noexcept { return data_[r * C + c]; }
T const& operator()(size_t const r, size_t const c) const noexcept { return data_[r * C + c]; }
static constexpr size_t Rows = R;
static constexpr size_t Columns = C;
private:
std::array<T, R * C> data_;
};
int main() {
matrix<int, 2, 3> m;
for (size_t i = 0; i < m.Rows; ++i)
for (size_t j = 0; j < m.Columns; ++j)
m(i, j) = i * m.Columns + (j + 1);
for (size_t i = 0; i < m.Rows; ++i) {
for (size_t j = 0; j < m.Columns; ++j)
std::cout << m(i, j) << ' ';
std::cout << '
';
}
}By overloading operator[] with two parameters, the same matrix can be accessed with a concise comma‑separated index list:
T& operator[](size_t const r, size_t const c) noexcept { return data_[r * C + c]; }
T const& operator[](size_t const r, size_t const c) const noexcept { return data_[r * C + c]; }
int main() {
matrix<int, 3, 2> m;
for (size_t i = 0; i < m.Rows; ++i)
for (size_t j = 0; j < m.Columns; ++j)
m[i, j] = i * m.Columns + (j + 1);
for (size_t i = 0; i < m.Rows; ++i) {
for (size_t j = 0; j < m.Columns; ++j)
std::cout << m[i, j] << ' ';
std::cout << '
';
}
}This overload makes multidimensional indexing as natural as the built‑in one‑dimensional case.
std::string/std::string_view contains() member
C++20 added starts_with() and ends_with() to std::basic_string and std::basic_string_view, but there was still no direct way to test whether a string contains a given substring. The usual approach was to call find() and compare the result with npos:
std::cout << (text.find("dolor") != std::string::npos) << '
';C++23 introduces a contains() member that returns a boolean indicating the presence of a substring or character, providing the same functionality with clearer syntax that aligns with starts_with() and ends_with():
std::cout << text.contains("dolor") << '
';Example usage of the new functions:
std::string text = "lorem ipsum dolor sit amet";
std::cout << std::boolalpha;
std::cout << text.starts_with("lorem") << '
'; // true
std::cout << text.starts_with("ipsum") << '
'; // false
std::cout << text.ends_with("dolor") << '
'; // false
std::cout << text.ends_with("amet") << '
'; // true
std::cout << text.contains("dolor") << '
'; // trueThese three C++23 enhancements—portable literal suffixes, a multidimensional subscript operator, and a convenient contains() method—address real‑world coding pain points and are expected to see frequent adoption.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Code DAO
We deliver AI algorithm tutorials and the latest news, curated by a team of researchers from Peking University, Shanghai Jiao Tong University, Central South University, and leading AI companies such as Huawei, Kuaishou, and SenseTime. Join us in the AI alchemy—making life better!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
