What is the purpose of std::rank?

Xzenon

I just ran into std::rank and I don't really understand what it could be used for. I understand what it does, but can someone please give me a few use cases for it? I wasn't able to find anything useful on a search here.

max66

Nice question.

I'm trying to learn C++11, so I could write something silly but ... suppose you want find the maximum value in a multidimensional array.

I tried to answer this question with std::rank (and SFINAE)

#include <iterator>
#include <iostream>
#include <type_traits>

template <typename X,
          typename = typename std::enable_if<0U == std::rank<X>::value>::type>
  X maxRank (X const & x)
 { return x; }

template <typename X,
          typename = typename std::enable_if<0U != std::rank<X>::value>::type>
typename std::remove_all_extents<X>::type maxRank (X const & x)
 {
   auto  it  = std::begin(x);
   auto  ret = maxRank(*it);

   for ( ; it != std::end(x) ; ++it )
    {
      auto val = maxRank(*it);

      if ( val > ret )
         ret = val;
    }

   return ret;
 }


int main ()
 {
   int  a0 = 12; 
   short a1[] = { 23, 7, 42, -19, 0, 95 };
   unsigned  a2[][2] = { {8U, 9U}, {0U, 77U}, {11U, 9U}, {5U, 3U} };
   long  a3[][3][2] = { { {123L, 3L}, {-45L, 77L}, {-12L, 678L} },
                        { {1L, -54L}, {23L, 99L}, {56L, 1234L} },
                        { {-4L, -12L}, {1L, 0L}, {122L, 19L} },
                        { {2L, 23L}, {55L, 19L}, {2L, 99L} } };

   std::cout << "maxRank a0 = " << maxRank(a0) << '\n';
   std::cout << "maxRank a1 = " << maxRank(a1) << '\n';
   std::cout << "maxRank a2 = " << maxRank(a2) << '\n';
   std::cout << "maxRank a3 = " << maxRank(a3) << '\n';

   return 0; 
 }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

What is the purpose of std::launder?

From Java

What's the purpose of std::to_integer?

From Dev

What is the real purpose of operator== for a std::function?

From Dev

What is the purpose of std::scoped_allocator_adaptor?

From Dev

What is the purpose of std::chrono::duration::zero()

From Dev

What's the purpose of declaring "namespace std {}"?

From Dev

What's the purpose of declaring "namespace std {}"?

From Dev

What is the purpose of the const overloads of std::begin and std::end?

From Java

What is the purpose of C++20 std::common_reference?

From Java

What is the purpose of std::forward()'s rvalue reference overload?

From Dev

What is the actual purpose of std::type_info::name()?

From Dev

What is the purpose of declaring an undefined std::function before its definition?

From Dev

What is the purpose and functioning of check(3rd Argument) in std::sort()?

From Dev

What is the purpose of .*\\?

From Dev

What is the purpose of "?"

From Dev

What is the purpose of using std::ios_base::trunc flag with std::ios_base::out

From Java

Docker, what is it and what is the purpose

From Java

What is the purpose of a unary "+" before a call to std::numeric_limits<unsigned char> members?

From Dev

Implementing std::rank for other containers

From Dev

Implementing std::rank for other containers

From Dev

What is purpose of dispatch sync?

From Java

What is the purpose of Angular animations?

From Java

What is the purpose of using HKDF?

From Dev

What is the purpose of MemoryCache in MVC?

From Dev

delphi: what the purpose of the icontent?

From Dev

what's the purpose of factories?

From Dev

What is the purpose of glClearDepth in OpenGL?

From Dev

What is the purpose of doOnNext(...) in RxJava

From Java

What is the purpose of VOLUME in Dockerfile

Related Related

  1. 1

    What is the purpose of std::launder?

  2. 2

    What's the purpose of std::to_integer?

  3. 3

    What is the real purpose of operator== for a std::function?

  4. 4

    What is the purpose of std::scoped_allocator_adaptor?

  5. 5

    What is the purpose of std::chrono::duration::zero()

  6. 6

    What's the purpose of declaring "namespace std {}"?

  7. 7

    What's the purpose of declaring "namespace std {}"?

  8. 8

    What is the purpose of the const overloads of std::begin and std::end?

  9. 9

    What is the purpose of C++20 std::common_reference?

  10. 10

    What is the purpose of std::forward()'s rvalue reference overload?

  11. 11

    What is the actual purpose of std::type_info::name()?

  12. 12

    What is the purpose of declaring an undefined std::function before its definition?

  13. 13

    What is the purpose and functioning of check(3rd Argument) in std::sort()?

  14. 14

    What is the purpose of .*\\?

  15. 15

    What is the purpose of "?"

  16. 16

    What is the purpose of using std::ios_base::trunc flag with std::ios_base::out

  17. 17

    Docker, what is it and what is the purpose

  18. 18

    What is the purpose of a unary "+" before a call to std::numeric_limits<unsigned char> members?

  19. 19

    Implementing std::rank for other containers

  20. 20

    Implementing std::rank for other containers

  21. 21

    What is purpose of dispatch sync?

  22. 22

    What is the purpose of Angular animations?

  23. 23

    What is the purpose of using HKDF?

  24. 24

    What is the purpose of MemoryCache in MVC?

  25. 25

    delphi: what the purpose of the icontent?

  26. 26

    what's the purpose of factories?

  27. 27

    What is the purpose of glClearDepth in OpenGL?

  28. 28

    What is the purpose of doOnNext(...) in RxJava

  29. 29

    What is the purpose of VOLUME in Dockerfile

HotTag

Archive