is_trivial function in C++

Trait class that identifies whether T is a trivial type. A trivial type is a type whose storage is contiguous (trivially copyable) and which only supports static default initialization (trivially default constructible), either cv-qualified or not. It includes scalar types, trivial classes and arrays of any such types.

A trivial class is a class (defined with class, struct or union) that is both trivially default constructible and trivially copyable, which implies that:

  • It uses the implicitly defined default, copy and move constructors, copy and move assignments, and destructor.
  • It has no virtual members.
  • It has no non-static data members with brace- or equal- initializers.
  • Its base class and non-static data members (if any) are themselves also trivial types.

is_trivial inherits from integral_constant as being either true_type or false_type, depending on whether T is a trivial type

Syntax :

template  struct is_trivial;

Example:

std::is_trivial::value
Here A is a class which has been passed as a parameter 
to the function is_trivial and it will return a value of
integral constant type bool i.e. either true or false
// CPP program to illustrate
// is_trivial function
#include <iostream>
#include <type_traits> //library containing is_trivial function
using namespace std;
class A {};
class B { B() {} };
class C : B {};
class D { virtual void fn() {} };
int main() {
  std::cout << std::boolalpha; //Returns value in boolean type
  std::cout << "A: " << std::is_trivial<A>::value << endl;
  std::cout << "B: " << std::is_trivial<B>::value << endl;
  std::cout << "C: " << std::is_trivial<C>::value << endl;
  std::cout << "D: " << std::is_trivial<D>::value << endl;
  return 0;
}

Output:

A: true
B: false
C: false
D: false


Disclaimer: This does not belong to TechCodeBit, its an article taken from the below
source and credits.
source and credits: http://www.geeksforgeeks.org
We have built the accelerating growth-oriented website for budding engineers and aspiring job holders of technology companies such as Google, Facebook, and Amazon
If you would like to study our free courses you can join us at

http://www.techcodebit.com. #techcodebit #google #microsoft #facebook #interview portal #jobplacements
#technicalguide

 

rakesh

Leave a Reply

Your email address will not be published. Required fields are marked *

Skip to toolbar