fork download
  1. #include <iostream>
  2. #include <typeinfo>
  3. #include <type_traits>
  4.  
  5. int main()
  6. {
  7. int a[20] = { 1, 2, 3, 4 } ; // array of 20 int
  8.  
  9. decltype(a) b = { 1, 2, 3, 4 } ; // b is of the same type as a - an array of 20 int
  10.  
  11. typedef int array_type[20] ; // 'array_type' is an alias for 'array of 20 int'
  12. using array_type = int[20] ; // 'array_type' is an alias for 'array of 20 int'
  13.  
  14. array_type c = { 1, 2, 3, 4 } ; // c is an array of 20 int
  15.  
  16. // b and c are of the same type
  17. std::cout << std::boolalpha ;
  18. std::cout << "b and c are of the same type: "
  19. << std::is_same< decltype(b), decltype(c) >::value << '\n' ; // true
  20.  
  21. array_type d[5] = { { 1, 2 }, {}, {6} } ; // array of 5 array_type
  22.  
  23. int e[5][20] = { { 1, 2 }, {}, {6} } ; // array of 5 array of 20 int
  24.  
  25. // d and e are of the same type
  26. std::cout << "d and e are of the same type: "
  27. << std::is_same< decltype(d), decltype(e) >::value << '\n' ; // true
  28.  
  29. int f[5][15] = { { 1, 2 }, {}, {6} } ; // array of 5 array of 15 int
  30.  
  31. // d and f are of different types
  32. std::cout << "d and f are of the same type: "
  33. << std::is_same< decltype(d), decltype(f) >::value << '\n' ; // false
  34.  
  35. // an (expression of type) array can be implicitly converted to a pointer,
  36. // the result of the conversion is a pointer to the first element of the array.
  37.  
  38. int* p1 = &(a[0]) ; // address of first element
  39. int* p2 = a ; // a is converted to a pointer, both p1 and p2 point to the first element
  40.  
  41. std::cout << "p1 == p2 : " << ( p1 == p2 ) << '\n' ; // true
  42. }
  43.  
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
b and c are of the same type: true
d and e are of the same type: true
d and f are of the same type: false
p1 == p2 : true