std::aligned_storage
来自cppreference.com
|
|
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
| Defined in header <type_traits>
|
||
| template< std::size_t Len, std::size_t Align = /*default-alignment*/ > struct aligned_storage; |
(C++11 起) | |
提供的的成员typedef
type,这是一个POD类型,适合使用作为未初始化的存储的任何对象,其大小是至多Len的对齐要求是Align的除数。默认值Align的任何对象,其大小是最Len对齐要求是最严格的(最大).Original:
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.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
目录 |
[编辑] 会员类型
| 姓名
Original: Name The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
Definition |
type
|
POD类型的大小
Len的对齐要求Align Original: the POD type of size Len with alignment requirement Align The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
[编辑] 注释
由std::aligned_storage定义的类型可以用来创建适合保存的给定类型的对象的未初始化的内存块,任选对齐比必要严格,例如在一个高速缓存或页边界.
Original:
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.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[编辑] 可能的实现
除了默认参数,aligned_storage是表达的alignas的
Original:
Except for default argument, aligned_storage is expressible in terms of alignas:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
template<std::size_t Len, std::size_t Align> struct aligned_storage { typedef struct { alignas(Align) unsigned char data[Len]; } type; }; |
[编辑] 为例
一个原始的静态向量类,,展示创建,访问,和破坏的对象的对齐存储
Original:
A primitive static vector class, demonstrating creation, access, and destruction of objects in aligned storage
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
#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'; }
Output:
***** **********
[编辑] 另请参阅
| alignas说明 | 指定的变量的存储空间应保持一致的具体数额(C++11)
Original: specifies that the storage for the variable should be aligned by specific amount (C++11) The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
| (C++11) |
获得该类型的对齐要求 Original: obtains the type's alignment requirements The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (类模板) |
| (C++11) |
适合所有类型的使用未初始化的存储定义的类型 Original: defines the type suitable for use as uninitialized storage for all given types The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (类模板) |
| (C++11) |
POD类型的对齐要求大,任何其他标量类型 Original: POD type with alignment requirement as great as any other scalar type The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (的typedef) |