Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/types/extent"

From cppreference.com
< cpp‎ | types
m (size_t for consistency (also used in gcc and libc++))
(simplify the example)
Line 38: Line 38:
 
#include <iostream>
 
#include <iostream>
 
#include <type_traits>
 
#include <type_traits>
 
template<typename A>
 
typename std::enable_if< std::rank<A>::value == 1 >::type print_1d(const A& a)
 
{
 
    for(std::size_t i = 0; i<std::extent<A>::value; ++i)
 
        std::cout << a[i] << ' ';
 
    std::cout << '\n';
 
}
 
  
 
int main()
 
int main()
 
{
 
{
     int a[][3] = {{1,2,3},{4,5,6}};
+
     std::cout << std::extent<int[3]>::value << '\n';
//  print_1d(a); // compile-time error
+
    std::cout << std::extent<int[3][4]>::value << '\n';
     print_1d(a[1]);
+
     std::cout << std::extent<int[3][4],1>::value << '\n';
     int* p = a[1];
+
     std::cout << std::extent<int[3][4],2>::value << '\n';
//  print_1d(p); // compile-time error
+
    std::cout << std::extent<int[]>::value << '\n';
 
}
 
}
 
  | output=
 
  | output=
4 5 6
+
3
 +
3
 +
4
 +
0
 +
0
 
}}
 
}}
  

Revision as of 11:02, 26 October 2011

Template:cpp/types/sidebar Template:ddcl list begin <tr class="t-dsc-header">

<td>
Defined in header <type_traits>
</td>

<td></td> <td></td> </tr> <tr class="t-dcl ">

<td >
template< class T, unsigned N = 0>
struct extent;
</td>

<td class="t-dcl-nopad"> </td> <td > Template:mark c++11 feature </td> </tr> Template:ddcl list end

If T is an array type, provides the member constant value equal to the number of elements along the Nth dimension of the array, if N is in [0, std::rank<T>::value). For any other type, or if T is array of unknown bound along its first dimension and N is 0, value is 0.

Contents

Inherited from std::integral_constant

Member constants

value
[static]
the number of elements along the Nth dimension of T
(public static member constant)

Member functions

operator std::size_t
converts the object to std::size_t, returns value
(public member function)
operator()
(C++14)
returns value
(public member function)

Member types

Type Definition
value_type std::size_t
type std::integral_constant<std::size_t, value>

Equivalent definition

Template:eq fun cpp

Example

Template:example cpp

See also

(C++11)
checks if a type is an array type
(class template) [edit]
(C++11)
obtains the number of dimensions of an array type
(class template) [edit]
removes one extent from the given array type
(class template) [edit]
removes all extents from the given array type
(class template) [edit]