Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/memory/assume aligned"

From cppreference.com
< cpp‎ | memory
m (langlinks)
(- nodiscard)
 
(5 intermediate revisions by 3 users not shown)
Line 3: Line 3:
 
{{ddcl|header=memory|since=c++20|1=
 
{{ddcl|header=memory|since=c++20|1=
 
template< std::size_t N, class T >
 
template< std::size_t N, class T >
[[nodiscard]] constexpr T* assume_aligned(T* ptr);
+
constexpr T* assume_aligned( T* ptr );
 
}}
 
}}
  
Informs the implementation that the object {{tt|ptr}} points to is aligned to at least {{tt|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 {{tt|assume_aligned}}.
+
Informs the implementation that the object {{c|ptr}} points to is aligned to at least {{tt|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 {{tt|assume_aligned}}.
  
The program is ill-formed if {{tt|N}} is not a power of 2. The behavior is undefined if {{tt|ptr}} does not point to an object of type {{tt|T}} (ignoring cv-qualification at every level), or if the object's alignment is not at least {{tt|N}}.
+
{{tt|N}} must be a power of 2. The behavior is undefined if {{c|ptr}} does not point to an object of type {{tt|T}} (ignoring cv-qualification at every level), or if the object's alignment is not at least {{tt|N}}.
  
=== Return value ===
+
===Return value===
{{tt|ptr}}.
+
{{c|ptr}}.
  
=== Exceptions ===
+
===Exceptions===
 
Throws nothing.
 
Throws nothing.
  
=== Notes ===
+
===Notes===
 
To ensure that the program benefits from the optimizations enabled by {{tt|assume_aligned}}, it is important to access the object via its return value:
 
To ensure that the program benefits from the optimizations enabled by {{tt|assume_aligned}}, it is important to access the object via its return value:
  
 
{{source|1=
 
{{source|1=
void f(int* p) {
+
void f(int* p)
  int* p1 = std::assume_aligned<256>(p);
+
{
  // Use p1, not p, to ensure benefit from the alignment assumption.
+
    int* p1 = std::assume_aligned<256>(p);
  // However, the program has undefined behavior if p is not aligned
+
    // Use p1, not p, to ensure benefit from the alignment assumption.
  // regardless of whether p1 is used.
+
    // 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 {{tt|assume_aligned}} does not cause the compiler to verify or enforce this.
 
It is up to the program to ensure that the alignment assumption actually holds. A call to {{tt|assume_aligned}} does not cause the compiler to verify or enforce this.
 +
 +
{{feature test macro|__cpp_lib_assume_aligned|{{tt|std::assume_aligned}}|value=201811L|std=C++20}}
 +
 +
===Example===
 +
{{example}}
  
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/language/dsc alignof}}
+
{{dsc inc|cpp/language/dsc alignof}}
{{dsc inc | cpp/language/dsc alignas}}
+
{{dsc inc|cpp/language/dsc alignas}}
{{dsc inc | cpp/types/dsc aligned_storage}}
+
{{dsc inc|cpp/types/dsc aligned_storage}}
{{dsc inc | cpp/memory/dsc align}}
+
{{dsc inc|cpp/memory/dsc align}}
 +
{{dsc inc|cpp/language/attributes/dsc assume}}
 
{{dsc end}}
 
{{dsc end}}
  
 
{{langlinks|es|ja|ru|zh}}
 
{{langlinks|es|ja|ru|zh}}

Latest revision as of 02:04, 1 July 2024

 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
Dynamic memory management
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Allocators
Garbage collection support
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)



 
Defined in header <memory>
template< std::size_t N, class T >
constexpr T* assume_aligned( T* ptr );
(since C++20)

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.

Contents

[edit] Return value

ptr.

[edit] Exceptions

Throws nothing.

[edit] 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.

Feature-test macro Value Std Feature
__cpp_lib_assume_aligned 201811L (C++20) std::assume_aligned

[edit] Example

[edit] See also

alignof operator(C++11) queries alignment requirements of a type[edit]
alignas specifier(C++11) specifies that the storage for the variable should be aligned by specific amount[edit]
(C++11)(deprecated in C++23)
defines the type suitable for use as uninitialized storage for types of given size
(class template) [edit]
(C++11)
aligns a pointer in a buffer
(function) [edit]
[[assume(expression)]](C++23) specifies that the expression will always evaluate to true at a given point
(attribute specifier)[edit]