Rank (computer programming): Difference between revisions

Content deleted Content added
No edit summary
Reformat code, add rank_v and fix rankof
Line 8:
 
<source lang="cpp">
#include <type_traits>
#include <cstddef>
Line 16 ⟶ 17:
* it is an array; zero otherwise (which is the usual convention)
*/
template <typename tT> struct rank
{
{ static const std::size_t value = 0; };
};
 
template<typename tT, std::size_t nN>
struct rank<tT[nN]>
{
{ static const std::size_t value = 1 + rank<tT>::value; };
};
 
template <typename t, std::size_t nT>
constexpr auto rank_v = rank<T>::value;
 
/* Rank of an expression
Line 27 ⟶ 35:
* Let the rank of an expression be the rank of its type
*/
 
template <typename t, std::size_t n>
template <typename T>
char(&rankof(t(&)[n]))[n];
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>>;
}
</source>
Line 35 ⟶ 50:
:<source lang="cpp">rank<T>::value</source>
or the shorter form
and the rank of an array-expression ''expr'' by
 
:<source lang="cpp">rank_v<T></source>
 
andCalculating the rank of an array-expression ''expr''can bybe done using
:<source lang="cpp">sizeof(rankof(expr))</source>
 
==See also==