Skip to content

Instantly share code, notes, and snippets.

@SavchenkoValeriy
Created October 15, 2019 22:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SavchenkoValeriy/978576a37c9b56a0a3320aa4c15227ff to your computer and use it in GitHub Desktop.
Save SavchenkoValeriy/978576a37c9b56a0a3320aa4c15227ff to your computer and use it in GitHub Desktop.
template <typename T, unsigned N>
struct MatrixImpl {
static_assert(N > 0, "Matrix should have at least one dimension");
using Type = std::vector<typename MatrixImpl<T, N - 1>::Type>;
};
template <typename T>
struct MatrixImpl<T, 1> {
using Type = std::vector<T>;
};
template <typename T, unsigned N>
using Matrix = typename MatrixImpl<T, N>::Type;
template <typename T>
auto createMatrix(T defaultValue) {
return defaultValue;
}
template <typename T, typename... Rest>
auto createMatrix(T defaultValue, unsigned N,
Rest... Dimensions) {
return Matrix<T, sizeof...(Rest) + 1>(
N, createMatrix<T>(defaultValue, Dimensions...)
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment