Namespaces
Variants

std:: aligned_storage

From cppreference.net
Metaprogramming library
Type traits
Type categories
(C++11)
(C++11) ( DR* )
Type properties
(C++11)
(C++11)
(C++14)
(C++11) (deprecated in C++26)
(C++11) ( until C++20* )
(C++11) (deprecated in C++20)
(C++11)
Type trait constants
Metafunctions
(C++17)
Supported operations
Relationships and property queries
Type modifications
Type transformations
aligned_storage
(C++11) (deprecated in C++23)
(C++11) (deprecated in C++23)
(C++11)
(C++11) ( until C++20* ) (C++17)

Compile-time rational arithmetic
Compile-time integer sequences
定义于头文件 <type_traits>
template < std:: size_t Len, std:: size_t Align = /* default-alignment */ >
struct aligned_storage ;
(C++11 起)
(C++23 中弃用)

提供嵌套类型 type ,该类型满足 TrivialType StandardLayoutType 要求,适用于作为任何对象的未初始化存储,该对象的大小不超过 Len 且其 对齐要求 Align 的约数。

Align 的默认值是对所有大小不超过 Len 的对象中最严格(最大)的对齐要求。若不使用默认值,则 Align 必须是某个类型 T alignof ( T ) 值,否则行为未定义。

Len == 0 时,行为是未定义的。

是否支持任何 扩展对齐 是由实现定义的。

如果程序为 std::aligned_storage 添加特化,则行为是未定义的。

目录

成员类型

名称 定义
type 一个至少具有 Len 大小且对齐要求为 Align 平凡 标准布局 类型

辅助类型

template < std:: size_t Len, std:: size_t Align = /* 默认对齐方式 */ >
using aligned_storage_t = typename aligned_storage < Len, Align > :: type ;
(自 C++14 起)
(C++23 中弃用)

注释

std::aligned_storage<>::type 定义的类型可用于创建未初始化的内存块,这些内存块适合存储给定类型的对象,并可选地以比其自然对齐要求更严格的方式对齐,例如在缓存行或页边界上对齐。

与任何其他未初始化存储一样,这些对象通过 placement new 创建,并通过显式析构函数调用销毁。

可能的实现

除了默认参数外,aligned_storage 可以通过 alignas 表达:

template<std::size_t Len, std::size_t Align = /* default alignment not implemented */>
struct aligned_storage
{
    struct type
    {
        alignas(Align) unsigned char data[Len];
    };
};

示例

一个基础的静态向量类,演示在对齐存储中对象的创建、访问和销毁。

#include <cstddef>
#include <iostream>
#include <new>
#include <string>
#include <type_traits>
template<class T, std::size_t N>
class static_vector
{
    // Properly aligned uninitialized storage for N T's
    std::aligned_storage_t<sizeof(T), alignof(T)> data[N];
    std::size_t m_size = 0;
public:
    // Create an object in aligned storage
    template<typename ...Args> void emplace_back(Args&&... args)
    {
        if (m_size >= N) // Possible error handling
            throw std::bad_alloc{};
        // Construct value in memory of aligned storage using inplace operator new
        ::new(&data[m_size]) T(std::forward<Args>(args)...);
        ++m_size;
    }
    // Access an object in aligned storage
    const T& operator[](std::size_t pos) const
    {
        // Note: std::launder is needed after the change of object model in P0137R1
        return *std::launder(reinterpret_cast<const T*>(&data[pos]));
    }
    // Destroy objects from aligned storage
    ~static_vector()
    {
        for (std::size_t pos = 0; pos < m_size; ++pos)
            // Note: std::launder is needed after the change of object model in P0137R1
            std::destroy_at(std::launder(reinterpret_cast<T*>(&data[pos])));
    }
};
int main()
{
    static_vector<std::string, 10> v1;
    v1.emplace_back(5, '*');
    v1.emplace_back(10, '*');
    std::cout << v1[0] << '\n' << v1[1] << '\n';
}

输出:

*****
**********

参见

alignas (C++11) 指定变量的存储应按特定量对齐
(说明符)
获取类型的对齐要求
(类模板)
分配对齐的内存
(函数)
(C++11 起) (C++23 中弃用)
定义适合用作所有给定类型的未初始化存储的类型
(类模板)
对齐要求与任何其他标量类型一样大的平凡类型
(类型定义)
(C++17)
指针优化屏障
(函数模板)