Open In App

is_standard_layout template in C++

Improve
Improve
Like Article
Like
Save
Share
Report

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.




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


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




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


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


Last Updated : 19 Nov, 2018
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads