Open In App

type_traits::alignment_of template in C++

Last Updated : 26 May, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

It is used to find the member constant value equal to the alignment requirement of the type T. If T is a reference type than it returns the alignment requirements of the type referred to. If T is an array type than it returns the alignment requirements of the element type.
Header File: 
 

#include<type_traits>

Syntax: 
 

template
  <class T>
  struct alignment_of;

Parameter: The template std::alignment_of accepts a single parameter T (Trait class).
Below programs illustrate the std::alignment_of template in C++ STL:
Program 1: 
 

CPP14




// C++ program to illustrate
// assignment_of template
 
#include <bits/stdc++.h>
#include <type_traits>
using namespace std;
 
class GFG {
};
 
// main method
int main()
{
    cout << alignment_of<GFG>::value << endl;
    cout << alignment_of<int>() << endl;
    cout << alignment_of<double>() << endl;
 
    return 0;
}


Output: 

1
4
8

 

Program 2: 
 

CPP14




// C++ program to illustrate
// assignment_of template
 
#include <bits/stdc++.h>
#include <type_traits>
using namespace std;
 
class GFG {
};
 
// main method
int main()
{
    cout << alignment_of<GFG>::value << endl;
    cout << alignment_of<float>() << endl;
    cout << alignment_of<char>() << endl;
 
    return 0;
}


Output: 

1
4
1

 

Reference: http://www.cplusplus.com/reference/type_traits/alignment_of/
 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads