std::assume_aligned(3)
π NAME π
std::assume_aligned β std::assume_aligned π
Okay so bestie, this is the std::assume_aligned page from the C++ Standard Libary manual (the header literally spells it βLibaryβ, and weβre keeping that energy π«Ά). Itβs a C++ standard library function, and itβs here to tell the implementation a little secret about alignment. Slay.
πΈ Synopsis πΈ
Defined in header <memory>
template< std::size_t N, class T > (since C++20)
[[nodiscard]] constexpr T* assume_aligned( T* ptr );
Hereβs the tea: std::assume_aligned informs the implementation that the object ptr points to is aligned to at least N.
The implementation may use this information to generate more efficient code, but it might only make this assumption if the object is accessed via the return value of assume_aligned.
N must be a power of 2. The behavior is undefined if ptr does not point to an object of type T (ignoring cv-qualification at every level), or if the objectβs alignment is not at least N. π
π¦ Return value π¦
ptr.
Thatβs the whole vibe: it returns ptr right back to you π
.
β¨ Exceptions β¨
Throws nothing. Not one exception, periodt. π
π Notes π
To ensure that the program benefits from the optimizations enabled by assume_aligned, it is important to access the object via its return value:
void f(int* p)
{
int* p1 = std::assume_aligned<256>(p);
// Use p1, not p, to ensure benefit from the alignment assumption.
// However, the program has undefined behavior if p is not aligned
// regardless of whether p1 is used.
}
It is up to the program to ensure that the alignment assumption actually holds. A call to assume_aligned does not cause the compiler to verify or enforce this. No cap: you have to keep that promise yourself. π«Ά
Feature-test macro
__cpp_lib_assume_alignedβ value201811L, std(C++20), featurestd::assume_aligned
π Example π
This section is incomplete.
Reason: no example.
π See also π
alignofoperator(C++11) β queries alignment requirements of a typealignasspecifier(C++11) β specifies that the storage for the variable should be aligned by specific amountaligned_storage(C++11)(deprecated in C++23) β defines the type suitable for use as uninitialized storage for types of given size (class template)align(C++11) β aligns a pointer in a buffer (function)- `[[assume(expression)]](C++23) β specifies that the expression will always evaluate to true at a given point (attribute specifier)
π Category: π
* Todo no example
Kisses from http://cppreference.com, version/date 2024.06.10, signed std::assume_aligned(3) β mwah πβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms