sizeof.cc 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // -*- mode: c++; coding: utf-8 -*-
  2. // ra-ra/test - What to expect of sizeof... no checks.
  3. // (c) Daniel Llorens - 2023
  4. // This library is free software; you can redistribute it and/or modify it under
  5. // the terms of the GNU Lesser General Public License as published by the Free
  6. // Software Foundation; either version 3 of the License, or (at your option) any
  7. // later version.
  8. #include "ra/test.hh"
  9. struct noargx {}; // Small uses noarg for its own
  10. struct sample1
  11. {
  12. uint8_t a[16];
  13. [[no_unique_address]] ra::Small<int, 0> b; // adds space for an int anyway :-/
  14. };
  15. struct sample2
  16. {
  17. uint8_t a[16];
  18. [[no_unique_address]] ra::Small<noargx, 1> b;
  19. };
  20. struct sample3
  21. {
  22. uint8_t a[16];
  23. [[no_unique_address]] int b[0]; // adds actually zero
  24. };
  25. struct sample4
  26. {
  27. uint8_t a[16];
  28. [[no_unique_address]] noargx b[1];
  29. };
  30. struct sample5
  31. {
  32. uint8_t a[16];
  33. [[no_unique_address]] std::array<int, 0> b; // adds one byte (array<T, 0> doesn't use T[0])
  34. };
  35. struct sample6
  36. {
  37. uint8_t a[16];
  38. [[no_unique_address]] std::array<noargx, 1> b;
  39. };
  40. int main()
  41. {
  42. ra::TestRecorder tr;
  43. tr.section("self");
  44. {
  45. std::cout << sizeof(std::array<ra::none_t, 9> {}) << std::endl;
  46. std::cout << sizeof(ra::none_t [9]) << std::endl;
  47. std::cout << sizeof(ra::noarg [9]) << std::endl;
  48. std::cout << sizeof(std::array<ra::none_t, 0> {}) << std::endl;
  49. std::cout << sizeof(ra::none_t [0]) << std::endl;
  50. std::cout << sizeof(ra::noarg [0]) << std::endl;
  51. std::cout << sizeof(ra::Small<int, 0>) << std::endl;
  52. std::cout << sizeof(ra::Small<ra::none_t, 0>) << std::endl;
  53. std::cout << sizeof(ra::Small<noargx, 0>) << std::endl;
  54. }
  55. tr.section("base class");
  56. {
  57. std::cout << sizeof(sample1) << std::endl;
  58. std::cout << sizeof(sample2) << std::endl;
  59. std::cout << sizeof(sample3) << std::endl;
  60. std::cout << sizeof(sample4) << std::endl;
  61. std::cout << sizeof(sample5) << std::endl;
  62. std::cout << sizeof(sample6) << std::endl;
  63. }
  64. }