fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <set>
  4. #include <array>
  5. using namespace std;
  6.  
  7. template<typename T>
  8. size_t metaSizeOf(const T& t)
  9. {
  10. return sizeof(T);
  11. }
  12.  
  13. // Pre-declarations, otherwise some resolutions could be missing
  14. template<typename T, typename Alloc, template <typename, typename> class V>
  15. size_t metaSizeOf(const V<T, Alloc>& v);
  16. template<typename T, typename Compare, typename Alloc, template <typename, typename, typename> class V>
  17. size_t metaSizeOf(const V<T, Compare, Alloc>& v);
  18. template<typename Key, typename Val, typename Compare, typename Alloc, template <typename, typename, typename, typename> class V>
  19. size_t metaSizeOf(const V<Key, Val, Compare, Alloc>& v);
  20. template<typename T, std::size_t N, template <typename, std::size_t> class V>
  21. size_t metaSizeOf(const V<T, N>& v);
  22. template<typename T, std::size_t N>
  23. size_t metaSizeOf(const std::array<T, N>&);
  24.  
  25.  
  26. // Implementation for std::vector, std::list, std::deque, etc.
  27. template<typename T, typename Alloc, template <typename, typename> class V>
  28. size_t metaSizeOf(const V<T, Alloc>& v)
  29. {
  30. size_t bytes = sizeof(V<T, Alloc>);
  31. for(const auto& t: v) bytes += metaSizeOf(t);
  32. return bytes;
  33. }
  34.  
  35. // Implementation for std::set, std::multiset, etc.
  36. template<typename T, typename Compare, typename Alloc, template <typename, typename, typename> class V>
  37. size_t metaSizeOf(const V<T, Compare, Alloc>& v)
  38. {
  39. size_t bytes = sizeof(V<T, Compare, Alloc>);
  40. for(const auto& t: v) bytes += metaSizeOf(t);
  41. return bytes;
  42. }
  43.  
  44. // Implementation for std::map, std::multimap, etc.
  45. template<typename Key, typename Val, typename Compare, typename Alloc, template <typename, typename, typename, typename> class V>
  46. size_t metaSizeOf(const V<Key, Val, Compare, Alloc>& v)
  47. {
  48. size_t bytes = sizeof(V<Key, Val, Compare, Alloc>);
  49. for(const auto& t: v) bytes += metaSizeOf(t);
  50. return bytes;
  51. }
  52.  
  53. // Implementation for std::array-like containers
  54. template<typename T, std::size_t N, template <typename, std::size_t> class V>
  55. size_t metaSizeOf(const V<T, N>& v)
  56. {
  57. size_t bytes = sizeof(V<T, N>);
  58. for(const auto& t: v) bytes += metaSizeOf(t);
  59. return bytes;
  60. }
  61.  
  62. // Implementation for std::array only
  63. template<typename T, std::size_t N>
  64. size_t metaSizeOf(const std::array<T, N>&)
  65. {
  66. return sizeof(std::array<T, N>) + N * sizeof(T);
  67. }
  68.  
  69.  
  70. int main() {
  71. std::vector<std::set<std::array<int, 10>>> v;
  72. // populate v
  73. std::array<int, 10> a;
  74. std::set<std::array<int, 10>> s;
  75. s.insert(a);
  76. v.push_back(s);
  77. v.push_back(s);
  78.  
  79. std::size_t bytes = sizeof(v);
  80. for(auto& s : v)
  81. {
  82. bytes += sizeof(s);
  83.  
  84. for(auto& a : s)
  85. {
  86. bytes += sizeof(a) + sizeof(int) * 10;
  87. }
  88. }
  89.  
  90. cout << "The size from for loop is " << bytes << endl;
  91. cout << "The size from metaSizeOf is " << metaSizeOf(v) << endl;
  92.  
  93. return 0;
  94. }
Success #stdin #stdout 0s 4176KB
stdin
Standard input is empty
stdout
The size from for loop is 280
The size from metaSizeOf is 280