# Rank (computer programming)

> Mediated Wiki article. Canonical URL: https://mediated.wiki/source/Rank_(computer_programming)
> Markdown URL: https://mediated.wiki/source/Rank_(computer_programming).md
> Source: https://en.wikipedia.org/wiki/Rank_(computer_programming)
> Source revision: 1274076977
> License: Creative Commons Attribution-ShareAlike 4.0 International (https://creativecommons.org/licenses/by-sa/4.0/)

{{More citation needed|date=February 2025}}

In [computer programming](/source/computer_programming), '''rank''' with no further specifications is usually a synonym for (or refers to) "number of dimensions";<ref>{{Cite web |title=Vocabulary_with_defintions |url=https://files.schudio.com/federation-of-boldmere-schools/files/documents/Vocabulary_with_defintions.docx}}</ref> thus, a two-dimensional array has rank ''two'', a three-dimensional array has rank ''three'' and so on.
Strictly, no formal definition can be provided which applies to every [programming language](/source/programming_language), since each of them has its own concepts, [semantics](/source/Formal_semantics_of_programming_languages) and terminology; the term may not even be applicable or, to the contrary, applied with a very specific meaning in the context of a given language.

In the case of [APL](/source/APL_programming_language) the notion applies to every operand; and [dyad](/source/Binary_function)s ("binary functions") have a ''left rank'' and a ''right rank''.

The box below instead shows how ''rank of a type'' and ''rank of an array expression'' could be defined (in a semi-formal style) for C++ and illustrates a simple way to calculate them at compile time.

<syntaxhighlight lang="cpp">
#include <type_traits>
#include <cstddef>
 
/* Rank of a type
 * -------------
 *
 * Let the rank of a type T be the number of its dimensions if
 * it is an array; zero otherwise (which is the usual convention)
 */
template <typename T> struct rank
{
    static const std::size_t value = 0;
};

template<typename T, std::size_t N>
struct rank<T[N]>
{
    static const std::size_t value = 1 + rank<T>::value;
};

template <typename T>
constexpr auto rank_v = rank<T>::value;

/* Rank of an expression
 *
 * Let the rank of an expression be the rank of its type
 */

template <typename T>
using unqualified_t = std::remove_cv_t<std::remove_reference_t<T>>; 

template <typename T>
auto rankof(T&& expr)
{
    return rank_v<unqualified_t<T>>;
}
</syntaxhighlight>
 
Given the code above the rank of a type T can be calculated at compile time by
 
:<syntaxhighlight lang="cpp">rank<T>::value</syntaxhighlight>
 
or the shorter form

:<syntaxhighlight lang="cpp">rank_v<T></syntaxhighlight>

Calculating the rank of an expression can be done using
 
:<syntaxhighlight lang="cpp">rankof(expr)</syntaxhighlight>

==See also==
*[Rank (linear algebra)](/source/Rank_(linear_algebra)), for a definition of ''rank'' as applied to [matrices](/source/matrix_(mathematics))
*[Rank (J programming language)](/source/Rank_(J_programming_language)), a concept of the same name in the [J programming language](/source/J_(programming_language))

== References ==
<references />{{DEFAULTSORT:Rank (Computer Programming)}}
Category:Arrays
Category:Programming language topics

{{Compu-lang-stub}}

---
Adapted from the Wikipedia article [Rank (computer programming)](https://en.wikipedia.org/wiki/Rank_(computer_programming)) by Wikipedia contributors ([contributor history](https://en.wikipedia.org/wiki/Rank_(computer_programming)?action=history)). Available under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). Changes may have been made.
