Rank (computer programming): Difference between revisions

Content deleted Content added
No edit summary
Line 6:
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.
 
<source lang="cpp">
<code>
#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; };
 
//* Rank of an expression
//*
//* Let the rank of an expression be the rank of its type
/*/
template <typename t, std::size_t n>
char(&rankof(t(&)[n]))[n];
</codesource>
Given the code above the rank of a type T can be calculated at compile time by
:<codesource lang="cpp">rank<T>::value</codesource>
and the rank of an array-expression ''expr'' by
:<codesource lang="cpp">sizeof(rankof(expr))</codesource>