fork download
  1. #include <iostream>
  2. #include <memory>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. template <size_t N, size_t... Sizes>
  8. struct total_extent {
  9. static constexpr size_t value = N * total_extent<Sizes...>::value;
  10. };
  11.  
  12. template <size_t N>
  13. struct total_extent<N> { static constexpr size_t value = N; };
  14.  
  15.  
  16. template <typename T, size_t... Sizes>
  17. struct CTensor
  18. {
  19. // embed data into structure rather than using pointer to heap
  20. T data[total_extent<Sizes...>::value];
  21. // can use here: std::array<T, total_extent<Sizes...>::value> data;
  22. };
  23.  
  24. template <typename T, size_t... Sizes>
  25. void pass_by_ref(CTensor<T, Sizes...> &tensor, const CTensor<T, Sizes...> &tensor2)
  26. {
  27. // to avoid copying pass by ref
  28. }
  29.  
  30. int main() {
  31. // should fit into stack
  32. CTensor<float, 3, 3, 3> stack_allocated;
  33.  
  34. // 64M floats better to allocate on heap
  35. auto heap_allocated = std::make_unique<CTensor<float, 256,256,256>>();
  36.  
  37. // and we still can embed it into other data structures
  38. std::vector<CTensor<float, 256, 256>> embedded_into_heap_allocated(2);
  39.  
  40. return 0;
  41. }
Success #stdin #stdout 0.07s 3264KB
stdin
Standard input is empty
stdout
Standard output is empty