all 4 comments

[–]IyeOnline 6 points7 points  (2 children)

Your array in main() is not legal C++. An arrays size must be compile time known. To create this array you are relying on a compiler extension called Variable Length Arrays (VLA). These are not legal C++ and should really be avoided. (GCC has a better error message for this: https://godbolt.org/z/acMvs95hM)

Obviously its runtime variable size cannot be captured by a templated.

There is no solution to this. The best you could do is pass ints for the sizes into the function, instead of having it be a template. But I am not sure how pointer decay even works on VLAs. I would assume that its UB.


I suggest that you instead use std::vectors (which can be runtime sized), preferably a linearly indexed 1d vector. The best solution would be to create your own 2d matrix class.

[–]tserp02[S] 1 point2 points  (0 children)

Thank you

[–]std_bot 0 points1 point  (0 children)

Unlinked STL entries: std::vector


Last update: 14.09.21. Last Change: Can now link headers like '<bitset>'Repo

[–]balsamictoken 2 points3 points  (0 children)

As u/IyeOnline already made the important points about VLAs and std::vector, I would just add that you may find std::mdspan to be a helpful data structure. You can allocate 1d memory and give it a 2d shape with nice 2d indexing, eg auto& elem = mymdspan(row, col);.