u8 string literal: code units, code points, and grapheme clusters
The `u8` prefix selects UTF-8 for a string literal. Its length in code units, its number of code points, and its number of grapheme clusters measure different things.
CKTIS / Knowledge base
Programming syntax, operators and concepts. Find an explanation, explore an example and return to learning.
The `u8` prefix selects UTF-8 for a string literal. Its length in code units, its number of code points, and its number of grapheme clusters measure different things.
Unary minus negates one operand. In `return -1;`, it produces an `int` value that this function uses to mean “not found”; it is not subtraction.
Unary minus computes the negative of one operand, whereas binary subtraction computes a difference. The promoted type determines what happens at the limits of its range.
Distinguish a literal’s base from its type and from the type used in arithmetic. A `u` suffix alone neither guarantees 32 bits nor forces byte-width arithmetic.
The suffix `u` or `U` selects an unsigned integer type, not a fixed width. `0u` and `1u` are `unsigned int`; a larger value can require a wider type.
`ULL` gives an integer literal the type `unsigned long long` when its value fits, which can change overload selection or how a bit shift is evaluated.
`= delete` prohibits use of a particular function or constructor. The declaration still participates in overload resolution and does not deallocate memory.
Disable copy construction and copy assignment separately, while letting the compiler define a virtual destructor for correct destruction through a base pointer.
UTF-8 text has different lengths depending on what you count: code units, code points or grapheme clusters. The example counts and truncates well-formed UTF-8 without splitting a code point.
Empty braces do not always mean value-initialization. The type determines whether they initialize a scalar, call a constructor, or apply the rules for aggregates.
A structured binding names the components of one object. Choosing `auto`, `auto&`, or `const auto&` changes how that object is stored or referred to.
Give the current `std::map` entry’s key and value separate names so you can read both in a range-based `for` loop without copying the pair.
A forwarding reference T&& lets a template accept lvalues and rvalues while retaining the information needed to forward the argument onward. It requires the appropriate deduction context; && alone is not enough.
A T& reference aliases an object or a function without making a copy. It cannot be reseated after initialization. For object types, const T& restricts modification through the reference and can also bind to a suitable temporary object.
A pointer to member identifies a non-static data member or member function without binding to a particular object. Declare it using C::*, then apply it to an object with .* or through an object pointer with ->*.
The `T*` declarator forms the type “pointer to T”. In `int* p = &value;`, `p` holds a pointer to `value`, not a copy of that integer. The position of `const` determines whether the restriction applies to the pointed-to object or to the pointer itself.
T&& declares an rvalue reference, a type distinct from T&. It binds to suitable rvalues and participates in move semantics. A reference name used as an expression is normally an lvalue. In C++23, however, a move-eligible name is an xvalue, as when the name of an rvalue-reference parameter referring to a non-volatile object type is used directly as the operand of a return statement. T&& with a deduced template parameter can be a forwarding reference.
alignas specifies an alignment requirement for a variable, data member, or class. It accepts a type or an integral constant expression and cannot weaken the natural alignment of the declared object or type.
`and` is an alternative spelling of `&&` in C++. The built-in operator tests whether both conditions are true and skips the right operand when the left operand is false.
and_eq is a reserved C++ keyword and an alternative spelling of &=. For built-in integral types, it performs bitwise AND and stores the result in the left operand. It is not the logical operator and.
The asm keyword introduces a declaration typically used to pass text to an assembler. Support is conditional and its meaning is implementation-defined, so it is not a portable way to execute processor instructions.
The auto keyword stands for a type deduced by the compiler from an initializer. It shortens declarations without losing static typing; writing auto& or const auto& specifies whether the declaration creates a reference.
`bitand` is a reserved alternative spelling of the `&` token in C++. It therefore means more than bitwise AND: depending on context, it can also take an address or declare an lvalue reference.
`bitor` is an alternative spelling of the C++ operator `|`. The built-in operator performs bitwise inclusive OR: a result bit is set when the corresponding bit of at least one operand is set. Do not confuse it with logical `or` (`||`).