fork(3) download
  1. #include <string>
  2. #include <iostream>
  3. #include <iterator>
  4. #include <type_traits>
  5.  
  6. template < typename T, std::size_t N >
  7. constexpr std::size_t size( T(&)[N] ) { return N ; }
  8.  
  9. int main()
  10. {
  11. const std::string first[] = { "a", "be", "see" } ;
  12.  
  13. std::cout << "array 'first' size: " << size(first) << ' ' ;
  14.  
  15. // std::end() overload for arrays uses the same technique
  16. std::cout << std::end(first) - std::begin(first) << ' ' ;
  17.  
  18. // more of the same
  19. std::cout << std::extent< decltype(first) >::value << '\n' ;
  20.  
  21.  
  22. const int second[][6] = { {0,1}, {2,3,4}, {5}, {6,7,8,9}, {} } ;
  23. using array_type = decltype(second) ;
  24.  
  25. std::cout << "array 'second' rank: " << std::rank<array_type>::value << ", "
  26. << "nrows: " << std::extent<array_type>::value << ", "
  27. << "ncols: " << std::extent<array_type,1>::value << '\n' ;
  28. }
  29.  
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
array 'first' size: 3 3 3
array 'second' rank: 2, nrows: 5, ncols: 6