#include<vector>
#include<iostream>
using std::size_t;
using std::cout;
// Creating a multidimensional container (e.g. `vector`, `map`)
template<template<typename...> class Container,
typename T,
size_t DIMENSION>
struct MultiDimensional
{
using internal = MultiDimensional<Container, T, DIMENSION-1>;
using type = Container<typename internal::type>;
template<typename... Args>
static
type Generate (const size_t size, Args... sizes)
{
return type(size, internal::Generate(sizes...));
}
};
// Last dimension overload
template<template<typename...> class Container,
typename T>
struct MultiDimensional<Container, T, 1>
{
using internal = T;
using type = Container<T>;
static
type Generate (const size_t size)
{
return type(size);
}
};
// Wrapper for the specific requirement of creating a multidimensional `std::vector`
template<typename T,
typename... Args>
auto make_vector(Args... sizes)
-> typename MultiDimensional<std::vector, T, sizeof...(sizes)>::type
{
return MultiDimensional<std::vector, T, sizeof...(sizes)>::Generate(sizes...);
}
int main ()
{
size_t n = 5;
auto v3 = make_vector<int>(n+1, n*2, n*3);
cout << "v3.size() = " << v3.size() << "\n";
cout << "v3.back().size() = " << v3.back().size() << "\n";
cout << "v3.back().back().size() = " << v3.back().back().size() << "\n";
}
I2luY2x1ZGU8dmVjdG9yPgojaW5jbHVkZTxpb3N0cmVhbT4KdXNpbmcgc3RkOjpzaXplX3Q7CnVzaW5nIHN0ZDo6Y291dDsKCi8vIENyZWF0aW5nIGEgbXVsdGlkaW1lbnNpb25hbCBjb250YWluZXIgKGUuZy4gYHZlY3RvcmAsIGBtYXBgKQp0ZW1wbGF0ZTx0ZW1wbGF0ZTx0eXBlbmFtZS4uLj4gY2xhc3MgQ29udGFpbmVyLAogICAgICAgICB0eXBlbmFtZSBULCAgCiAgICAgICAgIHNpemVfdCBESU1FTlNJT04+CnN0cnVjdCBNdWx0aURpbWVuc2lvbmFsCnsKICB1c2luZyBpbnRlcm5hbCA9IE11bHRpRGltZW5zaW9uYWw8Q29udGFpbmVyLCBULCBESU1FTlNJT04tMT47CiAgdXNpbmcgdHlwZSA9IENvbnRhaW5lcjx0eXBlbmFtZSBpbnRlcm5hbDo6dHlwZT47CgogIH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