type_traits::alignment_of template in C++

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

1
4
8

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

1
4
1

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.