std::aligned_storage

来自cppreference.com
TranslationBot留言 | 贡献2012年10月25日 (四) 20:00的版本 (Translated from the English version using Google Translate)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)

<metanoindex/>

 
 
 
 
在标头 <type_traits> 定义
template< std::size_t Len, std::size_t Align = /*default-alignment*/ >
struct aligned_storage;
(C++11 起)
。提供的的成员typedeftype,这是一个POD类型,适合使用作为未初始化的存储的任何对象,其大小是至多Len的对齐要求是Align的除数。默认值Align的任何对象,其大小是最Len对齐要求是最严格的(最大).
原文:
Provides the member typedef type, which is a POD type suitable for use as uninitialized storage for any object whose size is at most Len and whose alignment requirement is a divisor of Align. The default value of Align is the most stringent (the largest) alignment requirement for any object whose size is at most Len.
文本通过谷歌翻译机器翻译。
你可以帮忙校正和验证翻译。点击此处查看指示。

。会员类型。

。姓名。
原文:
Name
文本通过谷歌翻译机器翻译。
你可以帮忙校正和验证翻译。点击此处查看指示。
Definition
type
。 POD类型的大小Len的对齐要求Align
原文:
the POD type of size Len with alignment requirement Align
文本通过谷歌翻译机器翻译。
你可以帮忙校正和验证翻译。点击此处查看指示。

。注释。

。由std::aligned_storage定义的类型可以用来创建适合保存的给定类型的对象的未初始化的内存块,任选对齐比必要严格,例如在一个高速缓存或页边界.
原文:
The type defined by std::aligned_storage can be used to create uninitialized memory blocks suitable to hold the objects of given type, optionally aligned stricter than necessary, for example on a cache or page boundary.
文本通过谷歌翻译机器翻译。
你可以帮忙校正和验证翻译。点击此处查看指示。

。可能的实现。

。除了默认参数,aligned_storage是表达的alignas的。
原文:
Except for default argument, aligned_storage is expressible in terms of alignas:
文本通过谷歌翻译机器翻译。
你可以帮忙校正和验证翻译。点击此处查看指示。
template<std::size_t Len, std::size_t Align>
struct aligned_storage {
    typedef struct {
        alignas(Align) unsigned char data[Len];
    } type;
};

。为例。

。一个原始的静态向量类,,展示创建,访问,和破坏的对象的对齐存储。
原文:
A primitive static vector class, demonstrating creation, access, and destruction of objects in aligned storage
文本通过谷歌翻译机器翻译。
你可以帮忙校正和验证翻译。点击此处查看指示。

#include <iostream>
#include <type_traits>
#include <string>

template<class T, std::size_t N>
class static_vector
{
    // propertly aligned uninitialized storage for N T's
    typename std::aligned_storage <sizeof(T), std::alignment_of<T>::value>::type data[N];
    std::size_t m_size;
public:

    static_vector() : m_size(0) {};
    // Create an object in aligned storage
    template<typename ...Args> void emplace_back(Args&&... args) 
    {
        new(data+m_size) T(std::forward<Args>(args)...);
        m_size++; // bounds check omitted
    }

    // Access an object in aligned storage
    const T& operator[](size_t pos) const 
    {
        return reinterpret_cast<const T&>(data[pos]);
    }
    // Delete objects from aligned storage
    ~static_vector() 
    {
        for(std::size_t pos = 0; pos < m_size; ++pos) {
            reinterpret_cast<const T*>(data+pos)->~T();
        }
    }
};

int main()
{
    static_vector<std::string, 10> v1;
    v1.emplace_back(std::string(5, '*'));
    v1.emplace_back(std::string(10, '*'));
    std::cout << v1[0] << '\n' << v1[1] << '\n';
}

输出:

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

。另请参阅。

Template:cpp/language/dcl list alignasTemplate:cpp/types/dcl list alignment ofTemplate:cpp/types/dcl list aligned unionTemplate:cpp/types/dcl list max align t