std::meta::extent
From cppreference.com
| Defined in header <meta>
|
||
consteval std::size_t extent( std::meta::info r, unsigned i = 0 );
|
(since C++26) | |
If r represents an array type, returns the number of elements along the ith dimension of the array, if i is in range [0, std::meta::rank(^^r)). For any other type, or if r represents an array of unknown bound along its first dimension and i is 0, returns 0.
Equivalent to std::extent<T, I>::value, where T is the type represented by std::meta::dealias(r) and I is a constant equal to i.
Parameters
| r | - | a reflection value |
Return value
The number of elements along the specified dimension i, if r represents an array type and i is within the rank range. Otherwise 0.
Exceptions
std::meta::exception if r does not represent a type or type alias.
Example
Run this code
#include <meta>
static_assert
(""
&& extent(^^int[3]) == 3 // default dimension is 0
&& extent(^^int[3], 0) == 3 // dimension 0 has 3 elements
&& extent(^^int[3], 1) == 0 // dimension index is out of range
&& extent(^^int[3][4], 0) == 3
&& extent(^^int[3][4], 1) == 4
&& extent(^^int[3][4], 2) == 0 // dimension index is out of range
&& extent(^^int[]) == 0
);
int main()
{
const auto ext = extent(^^int[8]);
static_assert(ext == 8); // with implicit conversion to std::size_t
const int ints[]{1, 2, 3, 4};
static_assert(extent(^^decltype(ints)) == 4); // array size
[[maybe_unused]] int ary[][3] = { {1, 2, 3} };
// ary[0] is of type reference to int[3], so, the extent
// cannot be calculated correctly and it returns 0
static_assert(std::meta::is_same_type(^^decltype(ary[0]), ^^int(&)[3]));
static_assert(extent(^^decltype(ary[0])) == 0);
// removing reference gives correct extent value 3
static_assert(extent(std::meta::remove_cvref(^^decltype(ary[0]))) == 3);
}
See also
(C++26) |
checks if reflected type is an array type (function) |
(C++26) |
removes one extent from reflected array type (function) |
(C++26) |
removes all extents from reflected array type (function) |
(C++26) |
obtains the number of dimensions of reflected array type (function) |
(C++23) |
a descriptor of a multidimensional index space of some rank (class template) |
(C++11) |
obtains the size of an array type along a specified dimension (class template) |