Skip to content
Related Articles

Related Articles

std::extent() template in C++ with Examples
  • Last Updated : 28 May, 2020

The std::is_const template of C++ STL is present in the <type_traits> header file. If A is an array that has a rank greater than B, the extent is the bound of the Ath dimension and if B is zero and A is the array of unknown bound, than the extent value is zero.

Header File:

#include<type_traits>

Syntax:

template 
  <class A, unsigned B = 0>
  struct extent;

Parameters: It accepts the following parameters:

  • A : It represents a particular type.
  • B : It represents the dimensions for which the extent is to be obtained.

Below are the programs to demonstrate std::extent():



Program 1:




// C++ program to demonstrate
// std::extent()
  
#include <bits/stdc++.h>
#include <type_traits>
using namespace std;
  
// Driver Code
int main()
{
    // By default dimension is zero
    cout << extent<int[3]>::value << endl;
    cout << extent<int[3][4], 0>::value << endl;
    cout << extent<int[3][4], 1>::value << endl;
    cout << extent<int[3][4], 2>::value << endl;
    cout << extent<int[]>::value << endl;
  
    const int ints[] = { 1, 2, 3, 4 };
  
    // Used to print the size of array
    cout << extent<decltype(ints)>::value
         << endl;
  
    return 0;
}
Output:
3
3
4
0
0
4

Program 2:




// C++ program to demonstrate
// std::extent()
  
#include <bits/stdc++.h>
#include <type_traits>
using namespace std;
  
// Driver Code
int main()
{
    typedef int gfg[][50][70];
    cout << "gfg array (int[][50][70]):" << endl;
    cout << "rank: " << rank<gfg>::value << endl;
    cout << extent<gfg, 0>::value << endl;
    cout << extent<gfg, 1>::value << endl;
    cout << extent<gfg, 2>::value << endl;
    cout << extent<gfg, 3>::value << endl;
    return 0;
}
Output:
gfg array (int[][50][70]):
rank: 3
0
50
70
0

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

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
My Personal Notes arrow_drop_up
Recommended Articles
Page :