array-traits.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /* Descriptions of array-like objects.
  2. Copyright (C) 2019-2020 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. #ifndef GCC_ARRAY_TRAITS_H
  16. #define GCC_ARRAY_TRAITS_H
  17. /* Implementation for single integers (and similar types). */
  18. template<typename T, T zero = T (0)>
  19. struct scalar_array_traits
  20. {
  21. typedef T element_type;
  22. static const bool has_constant_size = true;
  23. static const size_t constant_size = 1;
  24. static const T *base (const T &x) { return &x; }
  25. static size_t size (const T &) { return 1; }
  26. };
  27. template<typename T>
  28. struct array_traits : scalar_array_traits<T> {};
  29. /* Implementation for arrays with a static size. */
  30. template<typename T, size_t N>
  31. struct array_traits<T[N]>
  32. {
  33. typedef T element_type;
  34. static const bool has_constant_size = true;
  35. static const size_t constant_size = N;
  36. static const T *base (const T (&x)[N]) { return x; }
  37. static size_t size (const T (&)[N]) { return N; }
  38. };
  39. #endif