Created
March 26, 2019 12:12
-
-
Save miquelramirez/05075b7909d1e4c5c4af0aac443be788 to your computer and use it in GitHub Desktop.
C++ generate tuple of arbitrary size initialised with default constructor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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