is_standard_layout template in C++


In this article we will be discussing the working, syntax and examples of std::is_standard_layout template in C++ STL.

is_standard_layout is a template which comes under the <type_traits> header file. This template is used to check whether the given type T is a standard layout or not.

What is a standard layout in C++?

Standard Layout type is a type in which is of simple linear structure (like we see in array) and access control which can be easily used to communicate with the program written in other programming languages. This is a type which is written with the idea of one code for all programming languages.

A standard layout class has the following features −

  • It shall have no virtual function or virtual classes.
  • Must have the same access control (public, private, protected) for all the classes.
  • Have no non-static data types.
  • Have no base class of the same type as its first non-static data type.

Syntax

template <class T>is_standard_layout;

Parameters

The template can have only parameter of type T, and check whether T is a standard layout type or not.

Return value

It returns a Boolean value, true if the given type is a standard layout type, and false if the given type is not a standard layout type.

Example

Input: is_standard_layout<int>::value;
Output: True

Input: class A{ int a; };
class B : public A
{ int b; };
is_standard_layout<B>::value;
Output: False

Example

 Live Demo

#include <iostream>
#include <type_traits>
using namespace std;
class TP {
   int var;
};
struct TP_2 {
   int var;
   private:
   int var_2;
};
union TP_3 {
};
int main() {
   cout << boolalpha;
   cout << "checking for is_standard_layout template for class : "<<
   is_standard_layout<TP>::value;
   cout << "\nchecking for is_standard_layout template for structure: "<<
   is_standard_layout<TP_2>::value;
   cout << "\nchecking for is_standard_layout template for union : "<<
   is_standard_layout<TP_3>::value;
   cout << "\nchecking for is_standard_layout template for char : "<<
   is_standard_layout<char>::value;
   cout << "\nchecking for is_standard_layout template for int : "<<
   is_standard_layout<int>::value;
   return 0;
}

Output

If we run the above code it will generate the following output −

checking for is_standard_layout template for class : true
checking for is_standard_layout template for structure: false
checking for is_standard_layout template for union : true
checking for is_standard_layout template for char : true
checking for is_standard_layout template for int : true

Example

 Live Demo

#include <iostream>
#include <type_traits>
using namespace std;
class TP {
   int var;
};
class TP_2 {
   int var;
   private:
   int var_2;
};
class TP_3 {
};
int main() {
   cout << boolalpha;
   cout << "checking for is_standard_layout template for class with one variable : "<<
   is_standard_layout<TP>::value;
   cout << "\nchecking for is_standard_layout template for class with one private variable: "<<
   is_standard_layout<TP_2>::value;
   cout << "\nchecking for is_standard_layout template for class with no variable : "<<
   is_standard_layout<TP_3>::value;
   cout << "\nchecking for is_standard_layout template for int * : "<<
   is_standard_layout<int*>::value;
   cout << "\nchecking for is_standard_layout template for float : "<<
   is_standard_layout<float>::value;
   return 0;
}

Output

If we run the above code it will generate the following output −

checking for is_standard_layout template for class with one variable : true
checking for is_standard_layout template for class with one private variable: true
checking for is_standard_layout template for class with no variable : true
checking for is_standard_layout template for int * : true
checking for is_standard_layout template for float : true

Updated on: 23-Mar-2020

175 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements