std::aligned_storage - cppreference.com
Da cppreference.com.
|
|
Questa pagina è stata tradotta in modo automatico dalla versione in ineglese della wiki usando Google Translate. La traduzione potrebbe contenere errori e termini strani. Muovi il puntatore sopra al testo per vedere la versione originale. Puoi aiutarci a correggere gli gli errori. Per ulteriori istruzioni clicca qui. |
<metanoindex/>
<tbody> </tbody>
| Elemento definito nell'header <type_traits> |
||
|
|
(dal C++11) | |
Fornisce il type membro typedef, che è un tipo POD adatto all'uso come memoria non inizializzata per qualsiasi oggetto la cui dimensione è al massimo e Len cui allineamento requisito è un divisore di Align. Il valore predefinito di Align è il requisito più rigoroso allineamento (il più grande) per qualsiasi oggetto la cui dimensione è al massimo 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.
Membri tipi
Nome Original: Name The text has been machine-translated via Google Translate. |
Definition |
type
|
il tipo di POD Original: the POD type of size The text has been machine-translated via Google Translate. |
Note
Tipo definito dal std::aligned_storage può essere usato per creare blocchi di memoria non inizializzate adatti a contenere gli oggetti di tipo determinato, eventualmente allineati rigorose del necessario, per esempio in una cache o limite di pagina.
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.
Possibile implementazione
Fatta eccezione per argomento di default, aligned_storage è esprimibile in termini di 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.
template<std::size_t Len, std::size_t Align> struct aligned_storage { typedef struct { alignas(Align) unsigned char data[Len]; } type; };
Esempio
Una classe primitiva vettore statico, dimostrando la creazione, l'accesso, e la distruzione di oggetti in memoria allineati
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.
#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:
Vedi anche
| alignas specificatore | specifica che il deposito per la variabile deve essere allineato (C++11) quantità specifica 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. |
(C++11) |
ottiene requisiti di allineamento del tipo Original: obtains the type's alignment requirements The text has been machine-translated via Google Translate. (classe template) [modifica] |
(C++11) |
definisce il tipo adatto per l'uso come memoria non inizializzata per tutti i tipi di dati Original: defines the type suitable for use as uninitialized storage for all given types The text has been machine-translated via Google Translate. (classe template) [modifica] |
(C++11) |
Tipo di POD con il requisito di allineamento così grande come qualsiasi altro scalare Original: POD type with alignment requirement as great as any other scalar type The text has been machine-translated via Google Translate. (typedef) [modifica] |