fork download
  1. #include <iostream>
  2. #include <array>
  3. #include <cstddef>
  4.  
  5. template <std::size_t W, std::size_t H>
  6. class Class {
  7. public:
  8. Class(const int (&_data)[H][W]) {
  9. for (std::size_t h = 0; h != H; ++h)
  10. for (std::size_t w = 0; w != W; ++w)
  11. data[h][w] = _data[h][w];
  12. }
  13.  
  14. template <typename... Args>
  15. Class(Args&&... args) {
  16. const std::array<int, H * W> temp{std::forward<Args>(args)...};
  17. for (std::size_t h = 0; h != H; ++h)
  18. for (std::size_t w = 0; w != W; ++w)
  19. data[h][w] = temp[h * W + w];
  20. }
  21.  
  22. void print() const {
  23. for (std::size_t y = 0; y != 2; ++y) {
  24. for (std::size_t x = 0; x != 3; ++x) {
  25. std::cout << data[y][x] << " ";
  26. }
  27. std::cout << std::endl;
  28. }
  29. }
  30. private:
  31. int data[H][W];
  32. };
  33.  
  34. int main() {
  35. Class<3, 2> c{ 1, 2, 3, 4, 5, 6 };
  36. c.print();
  37. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
1 2 3 
4 5 6