is_standard_layout template in C++

The std::is_standard_layout template of C++ STL is used to check whether the type is a standard layout or not. It returns a boolean value showing the same.

Syntax:

template < class T > struct is_standard_layout;

Parameters: This template contains single parameter T (Trait class) to check whether T is a standard layout type or not.

Return Value: This template returns a boolean value as shown below:

  • True: if the type is a standard layout type.
  • False: if the type is a non-standard layout type.

Below programs illustrate the std::is_standard_layout template in C++ STL:

Program 1: With Struct, Class and Union combined.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate
// std::is_standard_layout  template
  
#include <iostream>
#include <type_traits>
using namespace std;
  
// Class with local variable
class gfg {
    int variab;
};
  
// Structure with local variable
struct sam {
    int variab;
  
private:
    int variab_priv;
};
  
// Empty union
union raj {
};
  
// Driver code
int main()
{
    cout << boolalpha;
    cout << "Is gfg class a standard layout: "
         << is_standard_layout<gfg>::value << '\n';
    cout << "Is structure sam a standard layout: "
         << is_standard_layout<sam>::value << '\n';
    cout << "Is union raj a standard layout: "
         << is_standard_layout<raj>::value << '\n';
    cout << "Is datatype char a standard layout: "
         << is_standard_layout<char>::value << '\n';
    cout << "Is integer array 'int a[10]' a standard layout: "
         << is_standard_layout<int[10]>::value << '\n';
  
    return 0;
}

chevron_right


Output:

Is gfg class a standard layout: true
Is structure sam a standard layout: false
Is union raj a standard layout: true
Is datatype char a standard layout: true
Is integer array 'int a[10]' a standard layout: true

Program 2: With Class

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate
// std::is_standard_layout  template
  
#include <iostream>
#include <type_traits>
using namespace std;
  
// Class with local variable
class gfg {
    int variab;
};
  
// class with local variable
class sam {
    int variab;
  
private:
    int variab_priv;
};
  
// Empty class
class raj {
};
  
// Driver code
int main()
{
    cout << boolalpha;
    cout << "Is gfg class a standard layout: "
         << is_standard_layout<gfg>::value << '\n';
    cout << "Is structure sam a standard layout: "
         << is_standard_layout<sam>::value << '\n';
    cout << "Is union raj a standard layout: "
         << is_standard_layout<raj>::value << '\n';
    cout << "Is pointer 'int(gfg::*)' a standard layout: "
         << is_standard_layout<int(gfg::*)>::value << '\n';
    cout << "Is pointer 'int *' a standard layout: "
         << is_standard_layout<int*>::value << '\n';
    cout << "Is pointer 'nullptr_t' a standard layout: "
         << is_standard_layout<nullptr_t>::value << '\n';
  
    return 0;
}

chevron_right


Output:

Is gfg class a standard layout: true
Is structure sam a standard layout: true
Is union raj a standard layout: true
Is pointer 'int(gfg::*)' a standard layout: true
Is pointer 'int *' a standard layout: true
Is pointer 'nullptr_t' a standard layout: true


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.