std::meta::rank - cppreference.com
Namespaces
Variants

std::meta::rank

From cppreference.com
< cpp | meta
 
 
 
Reflection library
 
Reflection types and queries
Type properties
Type property queries
 
Defined in header <meta>
consteval std::size_t rank( std::meta::info r );
(since C++26)

Returns the number of dimensions of reflected array type represented by reflection r.

Equivalent to return std::rank<T>::value, where T is the type represented by std::meta::dealias(r).

Parameters

r - a reflection value

Return value

The number of dimensions of the array.

Exceptions

Throws std::meta::exception if r does not represent an array type.

Example

#include <meta>
#include <print>

static_assert(std::meta::rank(^^int) == 0);
static_assert(std::meta::rank(^^int[5]) == 1);
static_assert(std::meta::rank(^^int[5][5]) == 2);
static_assert(std::meta::rank(^^int[][5][5]) == 3);

int main()
{
    int a[][3]{{1, 2, 3}};
    std::println("{}", a);

    // The rank of reference type, e.g., a[0], that is int(&)[3], is 0:
    static_assert(std::meta::rank(^^decltype(a[0])) == 0);
    static_assert(std::meta::is_same_type(^^decltype(a[0]), ^^int(&)[3]));

    // The solution is to remove the reference type.
    static_assert(std::meta::rank(std::meta::remove_cvref(^^decltype(a[0]))) == 1);
}

Output:

[[1, 2, 3]]

See also