std::extent() template in C++ with Examples

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

3
3
4
0
0
4

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

gfg array (int[][50][70]):
rank: 3
0
50
70
0

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

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




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.