Skip to content
Related Articles

Related Articles

type_traits::alignment_of template in C++
  • Last Updated : 28 May, 2020

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:




// C++ program to illustrate
// assignment_of template
  
#include <bits/stdc++.h>
#include <type_traits>
using namespace std;
  
class GFG {
};
  
// main mehod
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:




// 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/

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
My Personal Notes arrow_drop_up
Recommended Articles
Page :