fork download
  1. #include <iostream>
  2. #include <type_traits>
  3.  
  4. template <typename T, typename... Params>
  5. struct TestTemplate
  6. {
  7. static constexpr size_t Sizeof()
  8. {
  9. return sizeof (typename std::remove_pointer<T>::type) + TestTemplate<Params...>::Sizeof();
  10. }
  11. };
  12.  
  13.  
  14. template <typename T>
  15. struct TestTemplate<T>
  16. {
  17. static constexpr size_t Sizeof()
  18. {
  19. return sizeof (typename std::remove_pointer<T>::type);
  20. }
  21. };
  22.  
  23. template <typename... Params>
  24. constexpr size_t GetSizeof (Params&&...)
  25. {
  26. return TestTemplate<Params...>::Sizeof();
  27. }
  28.  
  29.  
  30. struct TestType
  31. {
  32. double x = 0., y = 0.;
  33. char buf[64];
  34. };
  35.  
  36. int main ()
  37. {
  38. // assumint that sizeof(int) == 4
  39. static_assert(sizeof(int) == 4, "OP expect 32-bits int");
  40. static_assert(TestTemplate<int[10]>::Sizeof() == 40, "unexpected Sizeof");
  41. static_assert(GetSizeof (2, 3, 4) == 12, "unexpected Sizeof");
  42. TestType tt;
  43. static_assert(GetSizeof (&tt, 1) == 84, "unexpected Sizeof");
  44. int int_arr[10];
  45. static_assert(GetSizeof (int_arr, 1) == 44, "unexpected Sizeof");
  46.  
  47. std::cout << TestTemplate<int[10]>::Sizeof() << std::endl; // prints 40. OK
  48. std::cout << GetSizeof (2, 3, 4) << std::endl; // prints 12. OK
  49. std::cout << GetSizeof (&tt, 1) << std::endl; // prints 84. OK
  50. std::cout << GetSizeof (int_arr, 1) << std::endl; // prints 44 OK
  51.  
  52. const TestType* ctt = &tt;
  53. std::cout << GetSizeof (ctt, 1) << std::endl; // prints 8. Want 84.
  54. return 0;
  55. }
  56.  
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
40
12
84
44
8