名稱空間
變體
操作

std::aligned_storage

來自 cppreference.com
< cpp‎ | 型別
 
 
超程式設計庫
型別特性
型別類別
(C++11)
(C++11)(DR*)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11) 
(C++11)
(C++11)
型別屬性
(C++11)
(C++11)
(C++14)
(C++11)(C++26 中已棄用)
(C++11)(直到 C++20*)
(C++11)(C++20 中已棄用)
(C++11)
型別特性常量
元函式
(C++17)
支援的操作
關係與屬性查詢
型別修改
(C++11)(C++11)(C++11)
型別轉換
aligned_storage
(C++11)(C++23 中已棄用)
(C++11)(C++23 中已棄用)
(C++11)
(C++11)(直到 C++20*)(C++17)

(C++11)
(C++17)
編譯時有理數算術
編譯時整數序列
 
定義於標頭檔案 <type_traits>
template< std::size_t Len, std::size_t Align = /* 預設對齊 */ >
struct aligned_storage;
(C++11 起)
(C++23 中已棄用)

提供巢狀型別 type,它滿足 TrivialTypeStandardLayoutType,並且適合用作未初始化儲存,用於任何大小至多為 Len對齊要求Align 的除數的物件。

Align 的預設值是任何大小至多為 Len 的物件的最嚴格(最大)對齊要求。如果未使用預設值,則 Align 必須是某些型別 Talignof(T) 的值,否則行為未定義。

如果 Len == 0,則行為未定義。

是否支援任何 擴充套件對齊 是實現定義的。

如果程式為 std::aligned_storage 新增特化,則行為未定義。

目錄

[編輯] 成員型別

名稱 定義
型別 一個 平凡標準佈局 的型別,其大小至少為 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)
指標最佳化屏障
(函式模板) [編輯]
English 日本語 中文(简体) 中文(繁體)