# stdalign

***

**1.** Aligning a structure to the size of a double:

```cpp
struct AlignedStruct {
  double d;
  int i;
};

static_assert(alignof(AlignedStruct) == alignof(double));
```

**2.** Aligning a union to the size of its widest member:

```cpp
union AlignedUnion {
  double d;
  int i;
};

static_assert(alignof(AlignedUnion) == alignof(double));
```

**3.** Aligning an array to the size of its elements:

```cpp
int a[10];

static_assert(alignof(a) == alignof(int));
```

**4.** Aligning a pointer to the size of the type it points to:

```cpp
int* p;

static_assert(alignof(p) == alignof(int));
```

**5.** Aligning a function pointer to the size of a function pointer:

```cpp
int (*f)(int);

static_assert(alignof(f) == alignof(int (*)(int)));
```

**6.** Aligning a member function pointer to the size of a member function pointer:

```cpp
class C {
public:
  int (C::*mf)(int);
};

static_assert(alignof(C::*mf) == alignof(int (C::*)(int)));
```

**7.** Aligning a data member to a specific alignment:

```cpp
struct AlignedStruct {
  alignas(32) int i;
};

static_assert(alignof(AlignedStruct::i) == 32);
```

**8.** Aligning a base class to a specific alignment:

```cpp
class AlignedBase {
  alignas(64) int i;
};

class Derived : public AlignedBase {
  int j;
};

static_assert(alignof(Derived) == 64);
```

**9.** Aligning a virtual base class to a specific alignment:

```cpp
class AlignedBase {
  alignas(64) int i;
};

class Derived : virtual public AlignedBase {
  int j;
};

static_assert(alignof(Derived) == 64);
```

**10.** Aligning a function parameter to a specific alignment:

```cpp
void f(alignas(16) int i) {}

static_assert(alignof(i) == 16);
```

**11.** Aligning a function return value to a specific alignment:

```cpp
alignas(16) int f() { return 0; }

static_assert(alignof(f()) == 16);
```

**12.** Aligning a local variable to a specific alignment:

```cpp
void f() {
  alignas(16) int i;
  static_assert(alignof(i) == 16);
}
```

**13.** Aligning a catch clause to a specific alignment:

```cpp
try {
  throw 0;
} catch (alignas(16) int i) {}

static_assert(alignof(i) == 16);
```

**14.** Aligning a lambda capture to a specific alignment:

```cpp
auto f = [](alignas(16) int i) {};

static_assert(alignof(i) == 16);
```

**15.** Aligning a template parameter to a specific alignment:

```cpp
template <typename T, alignas(16) T* p>
void f() {}

static_assert(alignof(p) == 16);
```

**16.** Aligning a function template parameter to a specific alignment:

```cpp
template <typename F, alignas(16) F f>
void g() {}

static_assert(alignof(f) == 16);
```

**17.** Aligning a class template parameter to a specific alignment:

```cpp
template <typename T, alignas(16) class C>
void h() {}

static_assert(alignof(C) == 16);
```

**18.** Aligning a struct template parameter to a specific alignment:

```cpp
template <typename T, alignas(16) struct S>
void j() {}

static_assert(alignof(S) == 16);
```

**19.** Aligning an union template parameter to a specific alignment:

```cpp
template <typename T, alignas(16) union U>
void k() {}

static_assert(alignof(U) == 16);
```

**20.** Aligning an enum template parameter to a specific alignment:

```cpp
template <typename T, alignas(16) enum E>
void l() {}

static_assert(alignof(E) == 16);
```

**21.** Aligning a typedef to a specific alignment:

```cpp
typedef alignas(16) int AlignedInt;

static_assert(alignof(AlignedInt) == 16);
```

**22.** Using `std::align` to align a pointer:

```cpp
int* p = new std::align_val_t<16>;

static_assert(alignof(p) == 16);
```

**23.** Using `std::aligned_storage` to allocate aligned memory:

```cpp
std::aligned_storage<16, 16>::type buffer;

static_assert(alignof(buffer) == 16);
```

**24.** Using `std::aligned_union` to declare an aligned union:

```cpp
std::aligned_union<16, int, double> u;

static_assert(alignof(u) == 16);
```

**25.** Using `std::aligned_ptr` to declare an aligned pointer:

```cpp
std::aligned_ptr<16, int> p;

static_assert(alignof(*p) == 16);
```

**26.** Using `std::aligned_unique_ptr` to declare an aligned unique pointer:

```cpp
std::aligned_unique_ptr<16, int> p;

static_assert(alignof(*p) == 16);
```

**27.** Using `std::aligned_shared_ptr` to declare an aligned shared pointer:

```cpp
std::aligned_shared_ptr<16, int> p;

static_assert(alignof(*p) == 16);
```

**28.** Aligning a struct to the size of its largest member:

```cpp
struct MaxAlignStruct {
  int i;
  double d;
};

static_assert(alignof(MaxAlignStruct) == alignof(double));
```

**29.** Aligning a union to the size of its widest member:

```cpp
union MaxAlignUnion {
  int i;
  double d;
};

static_assert(alignof(MaxAlignUnion) == alignof(double

```
