Skip to content

Instantly share code, notes, and snippets.

@miquelramirez
Created March 26, 2019 12:12
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 miquelramirez/05075b7909d1e4c5c4af0aac443be788 to your computer and use it in GitHub Desktop.
Save miquelramirez/05075b7909d1e4c5c4af0aac443be788 to your computer and use it in GitHub Desktop.
C++ generate tuple of arbitrary size initialised with default constructor
#include <algorithm>
#include <tuple>
#include <iostream>
// That was an amazing display
//https://stackoverflow.com/a/37094024/338107
template<size_t, class T>
using T_ = T;
template<class T, size_t... Is>
auto gen(std::index_sequence<Is...>) { return std::tuple<T_<Is, T>...>{}; }
template<class T, size_t N>
auto gen() { return gen<T>(std::make_index_sequence<N>{}); }
int main()
{
constexpr unsigned N = 3;
auto tup = gen<int, N>();
auto [x, y, z] = tup;
std::cout << x << ", " << y << ", " << z << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment