Using C ++ 11:
template<typename T,size_t M,size_t N,size_t O>
using Matrix3D = std::array<std::array<std::array<T,O>,N>,M>;
std::unique_ptr<Matrix3D<double,200,200,200>> mat(new Matrix3D<double,200,200,200>);
(*mat)[m][n][o] = 10.0;
If you write a function make_unique, the variable declaration will be:
auto mat = std::make_unique<Matrix3D<double,200,200,200>>();
So, the whole program might look like this:
#include <memory> // std::unique_ptr for convenient and exception safe dynamic-memory management
#include <array> // std::array because it behaves much better than raw arrays
template<typename T,size_t M,size_t N,size_t O>
using Matrix3D = std::array<std::array<std::array<T,O>,N>,M>;
template<typename T,typename... Args>
std::unique_ptr<T> make_unique(Args&&... args) {
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
int main() {
auto mat = make_unique<Matrix3D<double,200,27,200>>();
(*mat)[199][26][199] = 10.0;
}
, ++ 11, , . , , , ( using Matrix3D =) - clang 3.0. GCC . Variadic ( make_unique) GCC, Clang, MSVC VS11.
, * ++ 11:
#include <memory>
#include <array>
template<typename T,size_t M,size_t N,size_t O>
struct Matrix3D {
std::array<std::array<std::array<T,O>,N>,M> array;
};
template<typename T>
std::unique_ptr<T> make_unique() {
return std::unique_ptr<T>(new T);
}
int main() {
auto mat = make_unique<Matrix3D<double,200,27,200>>();
mat->array[199][26][199] = 10.0;
}
* GCC MSVC.