2.6.8 Type Alignment

If you are the kind of person who write allocators or who use placement new in a buffer, then you will be happy to learn about alignas(T), which sets a type alignment to the value T, if it is an integer, or to match the alignment of the type T otherwise. Similarly, alignof(T) is the alignement of the type T.

char buffer[1024]; 
int consumed = 0; 
 
template<typename T> 
T* allocate() 
{ 
  const int shift = consumed % alignof(T); 
  const int offset = (shift == 0) ? 0 : (alignof(T) - shift); 
 
  T* const result = new (buffer + consumed + offset) T(); 
  consumed += offset + sizeof(T); 
 
  return result; 
}