Namespaces
Variants

std:: is_sufficiently_aligned

From cppreference.net
Memory management library
( exposition only* )
Allocators
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Memory resources
Uninitialized storage (until C++20)
( until C++20* )
( until C++20* )
( until C++20* )

Garbage collector support (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)
(C++11) (until C++23)
定义于头文件 <memory>
template < std:: size_t N, class T >
bool is_sufficiently_aligned ( T * ptr ) ;
(C++26 起)

检查指针 ptr 是否指向一个对齐值至少为 N 的对象。

ptr 未指向类型 T 的对象(忽略所有层级的 cv 限定符),则行为未定义。

目录

返回值

ptr 指向的对象具有至少 N 的对齐要求,则为 true ;否则为 false

异常

不抛出任何异常。

注释

std::is_sufficiently_aligned 可作为 std::assume_aligned 的前置条件使用。

功能测试 标准 功能
__cpp_lib_is_sufficiently_aligned 202411L (C++26) std::is_sufficiently_aligned

可能的实现

template<std::size_t N, class T>
bool is_sufficiently_aligned(T* ptr)
{
    return std::bit_cast<std::uintptr_t>(ptr) % N == 0;
}

示例

参见

alignof (C++11) 查询类型的对齐要求
(运算符)
alignas (C++11) 指定变量的存储应按特定量对齐
(说明符)
(since C++11) (deprecated in C++23)
定义适合用作给定大小类型的未初始化存储的类型
(类模板)
(C++11)
在缓冲区中对齐指针
(函数)
用于对齐访问 mdspan 元素的类型
(类模板)